<?php

namespace Database\Seeders;

use App\Models\Branch;
use App\Models\Category;
use App\Models\Company;
use App\Models\PaymentMethod;
use App\Models\Permission;
use App\Models\Role;
use App\Models\TaxRate;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DemoPosSeeder extends Seeder
{
    public function run(): void
    {
        $company = Company::query()->updateOrCreate(
            ['email' => 'admin@posbeast.test'],
            [
                'name' => 'PosBeast Demo Store',
                'legal_name' => 'PosBeast Demo Store Limited',
                'phone' => '+254700000001',
                'address' => 'Nairobi, Kenya',
                'country' => 'KE',
                'currency' => 'KES',
                'settings' => ['timezone' => 'Africa/Nairobi'],
            ]
        );

        $branch = Branch::query()->updateOrCreate(
            [
                'company_id' => $company->id,
                'code' => 'NBI-001',
            ],
            [
                'name' => 'Nairobi Main Branch',
                'address' => 'Kimathi Street, Nairobi',
                'phone' => '+254700000002',
                'email' => 'branch@posbeast.test',
                'county' => 'Nairobi',
                'is_active' => true,
            ]
        );

        $role = Role::query()->updateOrCreate(
            [
                'company_id' => $company->id,
                'slug' => 'administrator',
            ],
            [
                'name' => 'Administrator',
                'description' => 'Full access to the demo POS environment.',
            ]
        );

        $role->permissions()->sync(
            Permission::query()->pluck('id')->all()
        );

        User::query()->updateOrCreate(
            [
                'email' => 'admin@example.com',
            ],
            [
                'company_id' => $company->id,
                'branch_id' => $branch->id,
                'role_id' => $role->id,
                'name' => 'POS Administrator',
                'phone' => '+254700000003',
                'email_verified_at' => now(),
                'password' => Hash::make('password'),
                'pin' => Hash::make('1234'),
                'is_active' => true,
                'is_super_admin' => false,
            ]
        );

        User::query()->updateOrCreate(
            [
                'email' => 'superadmin@posbeast.test',
            ],
            [
                'company_id' => null,
                'branch_id' => null,
                'role_id' => null,
                'name' => 'Platform Super Admin',
                'phone' => '+254711111111',
                'email_verified_at' => now(),
                'password' => Hash::make('password'),
                'is_active' => true,
                'is_super_admin' => true,
            ]
        );

        foreach ([
            ['name' => 'Cash', 'type' => 'cash'],
            ['name' => 'M-Pesa', 'type' => 'mpesa'],
            ['name' => 'Card', 'type' => 'card'],
        ] as $method) {
            PaymentMethod::query()->updateOrCreate(
                [
                    'company_id' => $company->id,
                    'name' => $method['name'],
                ],
                [
                    'type' => $method['type'],
                    'is_active' => true,
                ]
            );
        }

        TaxRate::query()->updateOrCreate(
            [
                'company_id' => $company->id,
                'name' => 'VAT 16%',
            ],
            [
                'rate' => 16,
                'is_default' => true,
                'is_inclusive' => false,
                'is_active' => true,
            ]
        );

        foreach ([
            ['name' => 'Beverages', 'slug' => 'beverages'],
            ['name' => 'Groceries', 'slug' => 'groceries'],
            ['name' => 'Snacks', 'slug' => 'snacks'],
        ] as $category) {
            Category::query()->updateOrCreate(
                [
                    'company_id' => $company->id,
                    'slug' => $category['slug'],
                ],
                [
                    'name' => $category['name'],
                    'is_active' => true,
                ]
            );
        }
    }
}
