<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class UserRolePermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {

        // -------------------------
        // TRUNCATE TABLES
        // -------------------------
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        DB::table('role_has_permissions')->truncate();
        DB::table('model_has_permissions')->truncate();
        DB::table('model_has_roles')->truncate();

        Permission::truncate();
        Role::truncate();
        User::truncate();

        DB::statement('SET FOREIGN_KEY_CHECKS=1;');


        // -------------------------
        // CREATE ROLES
        // -------------------------
        $roles = [
            'super admin',
            'admin',
            'owner',
            'member'
        ];

        foreach ($roles as $role) {
            Role::firstOrCreate(['name' => $role]);
        }


        // -------------------------
        // CREATE PERMISSIONS
        // -------------------------
        $permissions = [
            // RBAC
            'view role',
            'create role',
            'update role',
            'delete role',

            'view permission',
            'create permission',
            'update permission',
            'delete permission',

            'view user',
            'create user',
            'update user',
            'delete user',

            // Audit Logs
            'view audit logs',
            'create audit logs',
            'update audit logs',
            'delete audit logs',

            // Subscription Marketplace
            'view subscription',
            'create subscription',
            'update subscription',
            'delete subscription',
            'join subscription',
            'update subscription member status',

            // Wallet / Payout
            'view wallet',
            'request payout',

            // Messaging
            'view message',
            'send message',

            // Profile
            'view profile',
            'update profile',

            // Referral / Trust
            'view referral',
            'view trust score',

            // Product
            'view products',
            'create products',
            'update products',
            'delete products',
            'publish products',

            // Categories / Brands
            'manage categories',
            'manage brands',

            // Specifications
            'manage specifications',
            'upload datasheet',

            // Stock
            'update stock',

            // Enquiries
            'view enquiries',
            'export enquiries',

            // Admin Controls
            'manage users',
            'manage roles',
            'manage permissions',
            'manage services',
            'manage plans',
            'manage payouts',
        ];

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


        // -------------------------
        // ASSIGN PERMISSIONS TO ROLES
        // -------------------------

        $superAdmin = Role::where('name','super admin')->first();
        $admin = Role::where('name','admin')->first();
        $owner = Role::where('name','owner')->first();
        $member = Role::where('name','member')->first();

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

        // Admin permissions
        $admin->syncPermissions([
            // RBAC
            'view role',
            'create role',
            'update role',
            'delete role',
            'view permission',
            'create permission',
            'update permission',
            'delete permission',
            'view user',
            'create user',
            'update user',
            'delete user',

            // Audit
            'view audit logs',
            'create audit logs',
            'update audit logs',
            'delete audit logs',

            // Marketplace
            'view subscription',
            'create subscription',
            'update subscription',
            'delete subscription',
            'join subscription',
            'update subscription member status',

            // User features
            'view wallet',
            'request payout',
            'view message',
            'send message',
            'view profile',
            'update profile',
            'view referral',
            'view trust score',

            // Legacy controls
            'view products',
            'create products',
            'update products',
            'delete products',
            'manage categories',
            'manage brands',
            'manage services',
            'manage plans',
            'manage payouts',
            'view enquiries'
        ]);

        // Owner permissions
        $owner->syncPermissions([
            'view profile',
            'update profile',
            'create subscription',
            'update subscription',
            'delete subscription',
            'view subscription',
            'join subscription',
            'update subscription member status',
            'view message',
            'send message',
            'view referral',
            'view trust score',
            'view wallet',
            'request payout'
        ]);

        // Member permissions
        $member->syncPermissions([
            'view profile',
            'update profile',
            'view subscription',
            'join subscription',
            'update subscription member status',
            'delete subscription',
            'view wallet',
            'view message',
            'send message',
            'view referral',
            'view trust score'
        ]);


        // -------------------------
        // CREATE USERS
        // -------------------------

        $superAdminUser = User::create([
            'name' => 'Super Admin',
            'email' => 'superadmin@example.com',
            'password' => Hash::make('12345678'),
        ]);

        $superAdminUser->assignRole('super admin');


        $adminUser = User::create([
            'name' => 'Admin',
            'email' => 'admin@example.com',
            'password' => Hash::make('12345678'),
        ]);

        $adminUser->assignRole('admin');


        $ownerUser = User::create([
            'name' => 'Subscription Owner',
            'email' => 'owner@example.com',
            'password' => Hash::make('12345678'),
        ]);

        $ownerUser->assignRole('owner');


        $memberUser = User::create([
            'name' => 'Member User',
            'email' => 'member@example.com',
            'password' => Hash::make('12345678'),
        ]);

        $memberUser->assignRole('member');


        $this->command->info('Roles, permissions, and default users seeded successfully.');
    }
}
