import 'dotenv/config';
import db from '../src/database/models/index.js';
import { ROLES } from '../src/common/config/constants.js';

const { DocumentStageConfig } = db;

const ALL_ROLES = Object.values(ROLES);

const configs = [
    // General / KYC Documents (Prospect/Dealer Initial)
    { documentType: 'PAN Card', stageCode: 'General', allowedRoles: ALL_ROLES, isMandatory: true },
    { documentType: 'GST Certificate', stageCode: 'General', allowedRoles: ALL_ROLES, isMandatory: true },
    { documentType: 'Aadhaar Card', stageCode: 'General', allowedRoles: ALL_ROLES, isMandatory: true },
    { documentType: 'Passport Size Photograph', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Partnership Deed', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'LLP Agreement', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Certificate of Incorporation', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Memorandum of Association (MOA)', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Articles of Association (AOA)', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Board Resolution', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Firm Registration Certificate', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Previous 6 Months Bank Statement', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'Cancelled Check', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'CIBIL Report (Self)', stageCode: 'General', allowedRoles: ALL_ROLES },
    { documentType: 'CIBIL Report (Firm)', stageCode: 'General', allowedRoles: ALL_ROLES },
    
    // Assessment / Interview Recommendation Documents
    { documentType: 'KT Matrix Scorecard', stageCode: 'Level 1 Interview', allowedRoles: [ROLES.RBM, ROLES.ASM, ROLES.DD_ZM, ROLES.SUPER_ADMIN], isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Panel Interview Evaluation Sheet', stageCode: 'Level 2 Interview', allowedRoles: [ROLES.ZBH, ROLES.DD_LEAD, ROLES.SUPER_ADMIN], isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'ZBH Recommendation Summary', stageCode: 'Level 2 Interview', allowedRoles: [ROLES.ZBH, ROLES.SUPER_ADMIN], module: 'ONBOARDING' },
    { documentType: 'Final Interview Recommendation Note', stageCode: 'Level 3 Interview', allowedRoles: [ROLES.NBH, ROLES.DD_HEAD, ROLES.SUPER_ADMIN], isMandatory: true, module: 'ONBOARDING' },
    
    // FDD (Financial Due Diligence) Specific
    { documentType: 'FDD Final Audit Report', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN, ROLES.NBH], isMandatory: true },
    { documentType: 'FDD Agency Assignment Letter', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Wealth Certificate', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Net Worth Statement', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'ITR Returns (Last 3 Years)', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Audited Balance Sheet', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Profit & Loss Statement', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Statutory Approval Certificate', stageCode: 'FDD', allowedRoles: [ROLES.FDD, ROLES.FINANCE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    
    // LOI / Security (Approval Process)
    { documentType: 'Security Deposit Receipt', stageCode: 'LOI Approval', allowedRoles: [ROLES.DEALER, ROLES.FINANCE, ROLES.DD_HEAD, ROLES.NBH, ROLES.SUPER_ADMIN], isMandatory: true },
    { documentType: 'First Fill Receipt', stageCode: 'LOA Approval', allowedRoles: [ROLES.DEALER, ROLES.FINANCE, ROLES.DD_HEAD, ROLES.NBH, ROLES.SUPER_ADMIN], isMandatory: true },
    { documentType: 'LOI Acknowledgement Copy', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES },
    { documentType: 'Nodal Agreement', stageCode: 'LOI Approval', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.DEALER, ROLES.SUPER_ADMIN] },
    { documentType: 'DIP Booklet', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Profile Sheet', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Dealership Application Form', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Interview Feedback Forms', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Land Selection Criteria Sheet', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Logic Note and Comparative Logic Note', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Zonal Evaluation Form', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Authorization Letter', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'City Map (PPT)', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Proposed Location Photos (minimum 20, PPT)', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Layout Drawings (PPT)', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Viability Sheet', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Project Plan', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Self-signed PAN/Aadhaar of all partners', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'CIBIL Reports of all partners', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Dealership Name & Address Email from RBM', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Rental / Lease Agreement or Consent Letter from Landlord', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Security Deposit Proof', stageCode: 'LOI Issue', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },

    // Architecture Team Documents
    { documentType: 'Architecture Assignment Document', stageCode: 'Architecture Team Assigned', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Architecture Blueprint (Site Layout)', stageCode: 'Architecture Document Upload', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN, ROLES.NBH] },
    { documentType: 'Site Plan (2D/3D)', stageCode: 'Architecture Document Upload', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Architecture Completion Certificate', stageCode: 'Architecture Team Completion', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN, ROLES.NBH] },
    { documentType: 'Pre-Construction Site Photos', stageCode: 'Architecture Team Assigned', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },
    { documentType: 'Post-Construction Site Photos', stageCode: 'Architecture Team Completion', allowedRoles: [ROLES.ARCHITECTURE, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN] },

    // EOR (Essential Operating Requirements)
    { documentType: 'Rental Agreement / Lease Deed', stageCode: 'EOR', allowedRoles: ALL_ROLES, isMandatory: true },
    { documentType: 'Property Ownership Documents / Index II', stageCode: 'EOR', allowedRoles: ALL_ROLES },
    { documentType: 'Fire NOC', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN], isMandatory: true },
    { documentType: 'Shop & Establishment License (Gumastha)', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN], isMandatory: true },
    { documentType: 'Trade License', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Pollution Control Board Certificate', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Electricity Bill / Load Enhancement NOC', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Water Connection NOC / Bill', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Other Supporting Document', stageCode: 'General', allowedRoles: ALL_ROLES, module: 'ONBOARDING' },
    { documentType: 'Other Supporting Document', stageCode: 'General', allowedRoles: ALL_ROLES, module: 'RESIGNATION' },
    { documentType: 'Other Supporting Document', stageCode: 'General', allowedRoles: ALL_ROLES, module: 'RELOCATION' },
    { documentType: 'Other Supporting Document', stageCode: 'General', allowedRoles: ALL_ROLES, module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'Other Supporting Document', stageCode: 'General', allowedRoles: ALL_ROLES, module: 'TERMINATION' },

    // Insurance Policy (Property & Stock) - EOR
    { documentType: 'Insurance Policy (Property & Stock)', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN], isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'Workshop Tooling & Equipment Invoice', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Signage & Visual Branding Photos', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN, ROLES.NBH] },
    { documentType: 'DMS Access Request Form', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    { documentType: 'Local Authority Approvals', stageCode: 'EOR', allowedRoles: [ROLES.DEALER, ROLES.DD_ADMIN, ROLES.SUPER_ADMIN] },
    
    // Final Steps
    { documentType: 'Inauguration Photos', stageCode: 'Inauguration', allowedRoles: ALL_ROLES },
    { documentType: 'Inauguration Report', stageCode: 'Inauguration', allowedRoles: ALL_ROLES },
    
    // Fallback
    // CONSTITUTIONAL_CHANGE Documents (Per ConstitutionalWorkflowService)
    { documentType: 'GST CERTIFICATE', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], isMandatory: true, module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'PAN CARD (OF PARTNERS/DIR)', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], isMandatory: true, module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'AADHAAR CARD (OF PARTNERS/DIR)', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'CANCELLED CHECK', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN, ROLES.FINANCE], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'PARTNERSHIP DEED', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'LLP AGREEMENT', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'MOA & AOA', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'CERTIFICATE OF INCORPORATION', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'BUSINESS PURCHASE AGREEMENT (BPA)', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'FIRM REGISTRATION CERTIFICATE', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },
    { documentType: 'AUTHORIZATION LETTER / DECLARATION', stageCode: 'Legal Review', allowedRoles: [ROLES.LEGAL_ADMIN, ROLES.SUPER_ADMIN], module: 'CONSTITUTIONAL_CHANGE' },

    // EOR (Essential Operating Requirements) - Extended for Relocation & Onboarding Audit
    { documentType: 'SALES STANDARDS COMPLIANCE', stageCode: 'EOR', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'SERVICE & SPARES READINESS', stageCode: 'EOR', allowedRoles: ALL_ROLES, isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'DMS INFRASTRUCTURE SETUP', stageCode: 'EOR', allowedRoles: [ROLES.DD_ADMIN, ROLES.SUPER_ADMIN, ROLES.ARCHITECTURE], isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'MANPOWER TRAINING CERTIFICATES', stageCode: 'EOR', allowedRoles: [ROLES.DD_ADMIN, ROLES.SUPER_ADMIN], module: 'ONBOARDING' },
    { documentType: 'INVENTORY FUNDING APPROVAL', stageCode: 'EOR', allowedRoles: [ROLES.FINANCE, ROLES.SUPER_ADMIN], isMandatory: true, module: 'ONBOARDING' },
    { documentType: 'MARKETING & WEBSITE DETAILS', stageCode: 'EOR', allowedRoles: ALL_ROLES, module: 'ONBOARDING' },
    { documentType: 'BPA (BUSINESS PURCHASE AGREEMENT)', stageCode: 'EOR', allowedRoles: ALL_ROLES, module: 'ONBOARDING' },
    
    // RELOCATION EOR Specific
    { documentType: 'NEW SITE LAYOUT / FLOOR PLAN', stageCode: 'EOR', allowedRoles: ALL_ROLES, isMandatory: true, module: 'RELOCATION' },
    { documentType: 'NOC FROM CURRENT LANDLORD', stageCode: 'EOR', allowedRoles: ALL_ROLES, isMandatory: true, module: 'RELOCATION' },
    { documentType: 'MUNICIPAL / FIRE SAFETY APPROVALS', stageCode: 'EOR', allowedRoles: ALL_ROLES, module: 'RELOCATION' },
    { documentType: 'LOCALITY MAP (LOCATION PIN)', stageCode: 'EOR', allowedRoles: ALL_ROLES, module: 'RELOCATION' },

    // RESIGNATION Documents
    { documentType: 'RESIGNATION LETTER (SIGNED COPY)', stageCode: 'Submission', allowedRoles: [ROLES.DEALER, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN], isMandatory: true, module: 'RESIGNATION' },
    { documentType: 'ASSET HANDOVER CERTIFICATE', stageCode: 'ZBH Review', allowedRoles: [ROLES.DEALER, ROLES.SUPER_ADMIN, ROLES.DD_ADMIN], isMandatory: true, module: 'RESIGNATION' },
    { documentType: 'NO DUES CERTIFICATE (FINANCE)', stageCode: 'Finance Review', allowedRoles: [ROLES.FINANCE, ROLES.SUPER_ADMIN], isMandatory: true, module: 'RESIGNATION' },
    { documentType: 'DEALER AGREEMENT TERMINATION DEED', stageCode: 'Approved', allowedRoles: [ROLES.SUPER_ADMIN, ROLES.LEGAL_ADMIN], module: 'RESIGNATION' },

    // TERMINATION Documents
    { documentType: 'SHOW CAUSE NOTICE (SCN)', stageCode: 'Hearing', allowedRoles: [ROLES.SUPER_ADMIN, ROLES.LEGAL_ADMIN], isMandatory: true, module: 'TERMINATION' },
    { documentType: 'DEALER EXPLANATION / RESPONSE', stageCode: 'Hearing', allowedRoles: [ROLES.DEALER, ROLES.SUPER_ADMIN, ROLES.LEGAL_ADMIN], module: 'TERMINATION' },
    { documentType: 'FINAL TERMINATION ORDER', stageCode: 'Closed', allowedRoles: [ROLES.SUPER_ADMIN, ROLES.LEGAL_ADMIN], module: 'TERMINATION' }
];

async function seed() {
    console.log('🌱 Updating Comprehensive Document Stage Configurations...');
    
    // Ensure table structure is updated
    await DocumentStageConfig.sync({ alter: true });
    
    // Clear old configs to avoid confusion with the fixed amount labels
    await DocumentStageConfig.destroy({ where: {} });

    for (const config of configs) {
        await DocumentStageConfig.create({
            ...config,
            isActive: true
        });
    }
    
    console.log('✅ Comprehensive document configs seeded!');
}

seed().catch(err => {
    console.error(err);
    process.exit(1);
}).then(() => process.exit(0));
