const mongoose = require('mongoose');

const roleSchema = new mongoose.Schema({
    name: {
        type: String,
        enum: ['admin', 'supervisor', 'inspector', 'client', 'technician', 'coordinator'],
        unique: true,
        required: true
    },
    displayName: {
        type: String,
        required: true
    },
    description: String,
    permissions: [{
        type: String,
        enum: [
            // Admin permissions
            'view_admin_dashboard',
            'manage_all_users',
            'manage_supervisors',
            'manage_inspectors',
            'manage_clients',
            'view_all_inspections',
            'manage_system_settings',
            'view_all_reports',
            'manage_payments',
            'manage_contracts',
            'view_audit_logs',
            'manage_backup',
            'view_analytics_dashboard',
            'manage_quality_control',
            'manage_predictive_analysis',
            
            // Supervisor permissions
            'view_supervisor_dashboard',
            'manage_assigned_inspectors',
            'review_inspections',
            'view_area_reports',
            'assign_inspections',
            'track_inspectors',
            
            // Inspector permissions
            'view_inspector_dashboard',
            'perform_inspections',
            'view_assigned_requests',
            'submit_inspection_reports',
            'view_own_performance',
            'use_dyno_test',
            'update_inspection_status',
            
            // Client permissions
            'view_client_dashboard',
            'create_requests',
            'view_own_vehicles',
            'manage_own_vehicles',
            'view_inspection_history',
            'make_payments',
            'view_contracts',
            'download_reports',
            
            // Coordinator permissions
            'view_coordinator_dashboard',
            'schedule_inspections',
            'assign_technicians',
            'manage_tracking',
            
            // Technician permissions
            'view_technician_dashboard',
            'perform_maintenance',
            'update_technical_data',
            
            // Common permissions
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications',
            'manage_notifications'
        ]
    }],
    level: {
        type: Number,
        default: 0 // Admin = 100, Supervisor = 80, Inspector = 60, Client = 20, etc.
    },
    isDefault: {
        type: Boolean,
        default: false
    }
}, {
    timestamps: true
});

// الصلاحيات الافتراضية لكل دور
const defaultRoles = {
    admin: {
        displayName: 'مدير النظام',
        level: 100,
        permissions: [
            'view_admin_dashboard',
            'manage_all_users',
            'manage_supervisors',
            'manage_inspectors',
            'manage_clients',
            'view_all_inspections',
            'manage_system_settings',
            'view_all_reports',
            'manage_payments',
            'manage_contracts',
            'view_audit_logs',
            'manage_backup',
            'view_analytics_dashboard',
            'manage_quality_control',
            'manage_predictive_analysis'
        ],
        isDefault: true
    },
    supervisor: {
        displayName: 'مشرف ميداني',
        level: 80,
        permissions: [
            'view_supervisor_dashboard',
            'manage_assigned_inspectors',
            'review_inspections',
            'view_area_reports',
            'assign_inspections',
            'track_inspectors',
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications',
            'manage_notifications'
        ],
        isDefault: true
    },
    inspector: {
        displayName: 'فاحص',
        level: 60,
        permissions: [
            'view_inspector_dashboard',
            'perform_inspections',
            'view_assigned_requests',
            'submit_inspection_reports',
            'view_own_performance',
            'use_dyno_test',
            'update_inspection_status',
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications',
            'manage_notifications'
        ],
        isDefault: true
    },
    client: {
        displayName: 'عميل',
        level: 20,
        permissions: [
            'view_client_dashboard',
            'create_requests',
            'view_own_vehicles',
            'manage_own_vehicles',
            'view_inspection_history',
            'make_payments',
            'view_contracts',
            'download_reports',
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications'
        ],
        isDefault: true
    },
    coordinator: {
        displayName: 'منسق',
        level: 70,
        permissions: [
            'view_coordinator_dashboard',
            'schedule_inspections',
            'assign_technicians',
            'manage_tracking',
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications',
            'manage_notifications'
        ],
        isDefault: true
    },
    technician: {
        displayName: 'فني',
        level: 40,
        permissions: [
            'view_technician_dashboard',
            'perform_maintenance',
            'update_technical_data',
            'view_profile',
            'update_profile',
            'change_password',
            'view_notifications'
        ],
        isDefault: true
    }
};

roleSchema.statics.seedDefaultRoles = async function() {
    for (const [roleName, roleData] of Object.entries(defaultRoles)) {
        await this.findOneAndUpdate(
            { name: roleName },
            { ...roleData, name: roleName },
            { upsert: true, new: true }
        );
    }
};

module.exports = mongoose.model('Role', roleSchema);