import { wfmFileService } from '../services/wfmFile.service';
import logger from '../utils/logger';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';

// Load environment variables
dotenv.config();

async function testWfmArchiveFinal() {
    try {
        console.log('Starting WFM Final Verification Test...');
        
        // 1. Test Claim CSV Archiving (GST)
        const claimData = [{
            TRNS_UNIQ_NO: 'TEST_FINAL_123',
            CLAIM_NUMBER: 'CLI_FINAL_001',
            DEALER_CODE: '6059',
            CLAIM_AMT: 1000.00
        }];

        const claimFileName = `ARCHIVE_FINAL_CLAIM_${Date.now()}.csv`;
        console.log(`Testing Claim CSV: ${claimFileName}`);
        const claimFilePath = await wfmFileService.generateIncomingClaimCSV(claimData, claimFileName, false); // false = GST
        
        console.log(`Main Claim CSV: ${claimFilePath}`);
        const expectedArchivePath = claimFilePath.replace('WFM_MAIN', 'WFM_ARACHIVE');
        console.log(`Archive Claim CSV expected at: ${expectedArchivePath}`);

        if (fs.existsSync(claimFilePath) && fs.existsSync(expectedArchivePath)) {
            console.log('✅ Claim CSV and Archive verified!');
        } else {
            if (!fs.existsSync(claimFilePath)) console.error('❌ Main Claim CSV not found!');
            if (!fs.existsSync(expectedArchivePath)) console.error('❌ Archive Claim CSV not found!');
        }

        // 2. Test Form 16 CSV Archiving (Credit)
        const form16Data = [{
            DEALER_CODE: '6059',
            QUARTER: 'Q1',
            YEAR: '2024-25',
            AMOUNT: 500.00
        }];

        const form16FileName = `ARCHIVE_FINAL_FORM16_${Date.now()}.csv`;
        console.log(`\nTesting Form 16 CSV: ${form16FileName}`);
        const form16FilePath = await wfmFileService.generateForm16IncomingCSV(form16Data, form16FileName, 'credit');
        
        console.log(`Main Form 16 CSV: ${form16FilePath}`);
        const expectedForm16ArchivePath = form16FilePath.replace('WFM_MAIN', 'WFM_ARACHIVE');
        console.log(`Archive Form 16 CSV expected at: ${expectedForm16ArchivePath}`);

        if (fs.existsSync(form16FilePath) && fs.existsSync(expectedForm16ArchivePath)) {
            console.log('✅ Form 16 CSV and Archive verified!');
        } else {
            if (!fs.existsSync(form16FilePath)) console.error('❌ Main Form 16 CSV not found!');
            if (!fs.existsSync(expectedForm16ArchivePath)) console.error('❌ Archive Form 16 CSV not found!');
        }

    } catch (error) {
        console.error('❌ Verification failed:', error);
    }
}

testWfmArchiveFinal();
