/**
 * Script to delete a test relocation request by requestId
 * Usage: npx tsx scripts/delete-test-relocation.ts REL-1775129490244-5B9C
 */
import db from '../src/database/models/index.js';

async function deleteRelocationRequest(requestId: string) {
    try {
        console.log(`Deleting relocation request: ${requestId}`);
        
        // Find the request
        const request = await db.RelocationRequest.findOne({
            where: { requestId }
        });
        
        if (!request) {
            console.log(`Request ${requestId} not found`);
            process.exit(0);
        }
        
        // Delete associated RequestParticipants
        await db.RequestParticipant.destroy({
            where: { requestId: request.id, requestType: 'relocation' }
        });
        console.log('Deleted associated participants');
        
        // Delete associated Worknotes
        await db.Worknote.destroy({
            where: { requestId: request.id, requestType: 'relocation' }
        });
        console.log('Deleted associated worknotes');
        
        // Delete the request
        await request.destroy();
        console.log(`Deleted relocation request: ${requestId}`);
        
        console.log('✅ Done!');
        process.exit(0);
    } catch (error) {
        console.error('❌ Error:', error);
        process.exit(1);
    } finally {
        await db.sequelize.close();
    }
}

const requestId = process.argv[2];
if (!requestId) {
    console.log('Usage: npx tsx scripts/delete-test-relocation.ts <requestId>');
    process.exit(1);
}

deleteRelocationRequest(requestId);