<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Seed essential data in correct order
        $this->call([
            RolesAndPermissionsSeeder::class,  // Must be first for user role assignment
            AccountSeeder::class,               // Chart of Accounts
            PartnerSeeder::class,               // Partner data
            CategorySeeder::class,              // Product categories
            PaymentMethodSeeder::class,         // Payment methods
            SettingsSeeder::class,              // System settings
        ]);

        // Create default branch (required for users)
        $defaultBranch = \App\Models\Branch::firstOrCreate(
            ['name' => 'Main Branch'],
            [
                'address' => 'Main Office',
                'phone' => '',
                'is_main' => true,
                'is_active' => true,
            ]
        );

        // Create default location for the branch
        \App\Models\Location::firstOrCreate(
            ['code' => 'MAIN-STORE'],
            [
                'branch_id' => $defaultBranch->id,
                'name' => 'Main Store',
                'name_ar' => 'المخزن الرئيسي',
                'is_default' => true,
                'is_active' => true,
                'sort_order' => 0,
            ]
        );

        // Create default super admin user
        $superAdmin = User::firstOrCreate(
            ['email' => 'admin@mobivault360.com'],
            [
                'name' => 'Super Admin',
                'password' => \Illuminate\Support\Facades\Hash::make('admin123'),
                'is_active' => true,
                'branch_id' => $defaultBranch->id,
            ]
        );
        $superAdmin->assignRole('super-admin');

        // Create test user for development
        if (app()->environment('local')) {
            $testUser = User::firstOrCreate(
                ['email' => 'test@example.com'],
                [
                    'name' => 'Test User',
                    'password' => \Illuminate\Support\Facades\Hash::make('password'),
                    'is_active' => true,
                    'branch_id' => $defaultBranch->id,
                ]
            );
            $testUser->assignRole('super-admin');
        }

        // Create specific admin user if not exists (for user convenience)
        $userAdmin = User::firstOrCreate(
            ['email' => 'admin@example.com'],
            [
                'name' => 'Admin User',
                'password' => \Illuminate\Support\Facades\Hash::make('password'),
                'is_active' => true,
                'branch_id' => $defaultBranch->id,
            ]
        );
        $userAdmin->assignRole('super-admin');
    }
}
