<?php

namespace Database\Seeders;

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

class RolesAndPermissionsSeeder extends Seeder
{
    public function run(): void
    {
        // Cache'i temizle
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // İzinler
        $permissions = [
            // İlan yönetimi
            'view job postings',
            'create job postings',
            'edit job postings',
            'delete job postings',
            'publish job postings',

            // Başvuru yönetimi
            'view applications',
            'edit application status',
            'add application notes',
            'export applications',

            // Departman yönetimi (granüler)
            'view departments',
            'create departments',
            'edit departments',
            'delete departments',

            // Görüşme yönetimi
            'view interviews',
            'manage interviews',

            // Site ayarları (granüler)
            'view site settings',
            'manage general settings',
            'manage google calendar settings',
            'manage virustotal settings',
            'manage system settings',
            'manage kvkk settings',

            // Kullanıcı yönetimi (granüler)
            'view users',
            'create users',
            'edit users',
            'delete users',

            // Log görüntüleme
            'view logs',
        ];

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

        // Roller
        $superAdmin = Role::firstOrCreate(['name' => 'super-admin']);
        $hrManager  = Role::firstOrCreate(['name' => 'hr-manager']);
        $hrViewer   = Role::firstOrCreate(['name' => 'hr-viewer']);

        // super-admin: tüm izinler
        $superAdmin->syncPermissions(Permission::all());

        // hr-manager: kullanıcı yönetimi ve log hariç her şey
        $hrManager->syncPermissions([
            'view job postings',
            'create job postings',
            'edit job postings',
            'delete job postings',
            'publish job postings',
            'view applications',
            'edit application status',
            'add application notes',
            'export applications',
            'view departments',
            'create departments',
            'edit departments',
            'delete departments',
            'view interviews',
            'manage interviews',
            'view site settings',
        ]);

        // hr-viewer: sadece görüntüleme
        $hrViewer->syncPermissions([
            'view job postings',
            'view applications',
            'view departments',
            'view interviews',
        ]);
    }
}