import 'dotenv/config';
import db from '../src/database/models/index.js';
import { PERMISSIONS, PERMISSION_CATEGORIES } from '../src/common/config/permissions.js';

const { Permission } = db;

const permissionsToSeed = [
    // Action Permissions
    {
        code: PERMISSIONS.ACTION_APPROVE,
        name: 'Approve Applications',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'APPROVE',
        description: 'Ability to approve applications'
    },
    {
        code: PERMISSIONS.ACTION_REJECT,
        name: 'Reject Applications',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'REJECT',
        description: 'Ability to reject applications'
    },
    {
        code: PERMISSIONS.ACTION_UPLOAD_DOCS,
        name: 'Upload Documents',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'UPLOAD_DOCS',
        description: 'Ability to upload documents'
    },
    {
        code: PERMISSIONS.ACTION_REQUEST_CHANGES,
        name: 'Request Changes',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'REQUEST_CHANGES',
        description: 'Ability to request changes'
    },
    {
        code: PERMISSIONS.ACTION_FORWARD,
        name: 'Forward to Others',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'FORWARD',
        description: 'Ability to forward applications'
    },
    {
        code: PERMISSIONS.ACTION_REASSIGN,
        name: 'Reassign Applications',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'REASSIGN',
        description: 'Ability to reassign applications'
    },
    {
        code: PERMISSIONS.ACTION_SCHEDULE_INTERVIEW,
        name: 'Schedule Interviews',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'SCHEDULE_INTERVIEW',
        description: 'Ability to schedule interviews'
    },
    {
        code: PERMISSIONS.ACTION_ADD_COMMENTS,
        name: 'Add Comments/Notes',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'ADD_COMMENTS',
        description: 'Ability to add comments'
    },
    {
        code: PERMISSIONS.ACTION_RANK_APPLICANTS,
        name: 'Rank Applicants',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'RANK_APPLICANTS',
        description: 'Ability to rank applicants'
    },
    {
        code: PERMISSIONS.ACTION_FINAL_APPROVAL,
        name: 'Final Approval',
        category: PERMISSION_CATEGORIES.ACTION,
        module: 'APPLICATIONS',
        type: 'ACTION',
        action: 'FINAL_APPROVAL',
        description: 'Ability to give final approval'
    },

    // View Permissions
    {
        code: PERMISSIONS.VIEW_DETAILS,
        name: 'Application Details',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_DETAILS',
        description: 'View basic application details'
    },
    {
        code: PERMISSIONS.VIEW_FINANCIAL,
        name: 'Financial Information',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_FINANCIAL',
        description: 'View financial information'
    },
    {
        code: PERMISSIONS.VIEW_DISCUSSIONS,
        name: 'Discussion Notes',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_DISCUSSIONS',
        description: 'View internal discussion notes'
    },
    {
        code: PERMISSIONS.VIEW_PROGRESS,
        name: 'Progress Tracking',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_PROGRESS',
        description: 'View application progress'
    },
    {
        code: PERMISSIONS.VIEW_AUDIT,
        name: 'Audit Logs',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_AUDIT',
        description: 'View application audit logs'
    },
    {
        code: PERMISSIONS.VIEW_DOCUMENTS,
        name: 'All Documents',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_DOCUMENTS',
        description: 'View all documents'
    },
    {
        code: PERMISSIONS.VIEW_PERSONAL,
        name: 'Personal Information',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_PERSONAL',
        description: 'View applicant personal info'
    },
    {
        code: PERMISSIONS.VIEW_BUSINESS,
        name: 'Business Details',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_BUSINESS',
        description: 'View applicant business details'
    },
    {
        code: PERMISSIONS.VIEW_REPORTS,
        name: 'Reports & Analytics',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_REPORTS',
        description: 'View application reports'
    },
    {
        code: PERMISSIONS.VIEW_HISTORY,
        name: 'Application History',
        category: PERMISSION_CATEGORIES.VIEW,
        module: 'APPLICATIONS',
        type: 'VIEW',
        action: 'VIEW_HISTORY',
        description: 'View status history'
    },

    // Stage Permissions
    {
        code: PERMISSIONS.STAGE_INITIAL_REVIEW,
        name: 'Initial Review',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Initial Review stage'
    },
    {
        code: PERMISSIONS.STAGE_FIELD_VERIFICATION,
        name: 'Field Verification',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Field Verification stage'
    },
    {
        code: PERMISSIONS.STAGE_LEVEL1_INTERVIEW,
        name: 'Level 1 Interview',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Level 1 Interview stage'
    },
    {
        code: PERMISSIONS.STAGE_LEVEL2_INTERVIEW,
        name: 'Level 2 Interview',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Level 2 Interview stage'
    },
    {
        code: PERMISSIONS.STAGE_RANKING,
        name: 'Ranking & Selection',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Ranking & Selection stage'
    },
    {
        code: PERMISSIONS.STAGE_LEGAL_REVIEW,
        name: 'Legal Review',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Legal Review stage'
    },
    {
        code: PERMISSIONS.STAGE_FINANCIAL_REVIEW,
        name: 'Financial Review',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Financial Review stage'
    },
    {
        code: PERMISSIONS.STAGE_FINAL_APPROVAL,
        name: 'Final Approval',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Final Approval stage'
    },
    {
        code: PERMISSIONS.STAGE_PAYMENT,
        name: 'Payment Verification',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Payment Verification stage'
    },
    {
        code: PERMISSIONS.STAGE_ONBOARDING,
        name: 'Onboarding',
        category: PERMISSION_CATEGORIES.STAGE,
        module: 'WORKFLOW',
        type: 'STAGE_ACCESS',
        action: 'ACCESS',
        description: 'Access to Onboarding stage'
    }
];

async function seedPermissions() {
    console.log('🌱 Seeding permissions...');
    try {
        await db.sequelize.authenticate();

        for (const p of permissionsToSeed) {
            await Permission.upsert({
                permissionCode: p.code,
                permissionName: p.name,
                permissionCategory: p.category,
                module: p.module,
                permissionType: p.type,
                action: p.action,
                description: p.description
            });
        }

        console.log('✅ Permissions seeded successfully!');
        process.exit(0);
    } catch (error) {
        console.error('❌ Seeding failed:', error);
        process.exit(1);
    }
}

seedPermissions();
