<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class RoleAndPermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        // Create Permissions
        $permissions = [
            // Attendance
            'attendance.view_own', 'attendance.view_all', 'attendance.checkin', 'attendance.manual_entry', 'attendance.export',
            // Work Reports
            'report.view_own', 'report.view_all', 'report.submit', 'report.approve', 'report.edit_any',
            // Projects
            'project.view_own', 'project.view_all', 'project.create', 'project.edit', 'project.delete',
            // Tasks
            'task.view_own', 'task.view_all', 'task.create', 'task.edit', 'task.delete',
            'task.assign_self', 'task.assign_others', 'task.comment', 'task.attach_files',
            // Leaves
            'leave.view_own', 'leave.view_all', 'leave.apply', 'leave.approve',
            // Users
            'user.view', 'user.create', 'user.edit', 'user.deactivate',
            // Departments
            'department.view', 'department.create', 'department.edit', 'department.delete',
            // Announcements
            'announcement.view', 'announcement.create', 'announcement.edit', 'announcement.delete',
            // Settings
            'settings.view', 'settings.edit',
        ];

        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission]);
        }

        // Roles
        $superAdmin = Role::firstOrCreate(['name' => 'super_admin']);
        $admin      = Role::firstOrCreate(['name' => 'admin']);
        $manager    = Role::firstOrCreate(['name' => 'manager']);
        $employee   = Role::firstOrCreate(['name' => 'employee']);

        // Super Admin gets all permissions
        $superAdmin->syncPermissions(Permission::all());

        // Admin Permissions
        $admin->syncPermissions([
            'attendance.view_all', 'attendance.manual_entry', 'attendance.export',
            'report.view_all', 'report.approve', 'report.edit_any',
            'project.view_all', 'project.create', 'project.edit', 'project.delete',
            'task.view_all', 'task.create', 'task.edit', 'task.delete', 'task.assign_others',
            'task.comment', 'task.attach_files',
            'leave.view_all', 'leave.approve',
            'user.view', 'user.create', 'user.edit', 'user.deactivate',
            'department.view', 'department.create', 'department.edit', 'department.delete',
            'announcement.view', 'announcement.create', 'announcement.edit', 'announcement.delete',
            'settings.view', 'settings.edit',
        ]);

        // Manager Permissions
        $manager->syncPermissions([
            'attendance.view_own', 'attendance.view_all',
            'report.view_own', 'report.view_all',
            'project.view_all', 'project.create', 'project.edit',
            'task.view_all', 'task.create', 'task.edit', 'task.assign_others',
            'task.comment', 'task.attach_files',
            'leave.view_own', 'leave.view_all',
            'user.view', 'announcement.view', 'department.view',
            'attendance.checkin',
        ]);

        // Employee Permissions
        $employee->syncPermissions([
            'attendance.view_own', 'attendance.checkin',
            'report.view_own', 'report.submit',
            'project.view_own',
            'task.view_own', 'task.create', 'task.assign_self', 'task.assign_others',
            'task.comment', 'task.attach_files',
            'leave.view_own', 'leave.apply',
            'announcement.view',
        ]);
    }
}
