<?php

namespace Database\Seeders;

use App\Models\Currency;
use App\Models\Customer;
use App\Models\ExpenseCategory;
use App\Models\FuelQuality;
use App\Models\FuelType;
use App\Models\Role;
use App\Models\Setting;
use App\Models\Shift;
use App\Models\Supplier;
use App\Models\Tank;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $allPermissions = array_keys(Role::permissionList());

        $adminRole = Role::updateOrCreate(
            ['code' => 'admin'],
            ['name' => 'Admin', 'permissions' => $allPermissions, 'is_active' => true]
        );

        $cashierRole = Role::updateOrCreate(
            ['code' => 'cashier'],
            ['name' => 'Cashier / POS User', 'permissions' => ['dashboard','pos_sale','sales_list','cash'], 'is_active' => true]
        );

        $stockRole = Role::updateOrCreate(
            ['code' => 'stock_officer'],
            ['name' => 'Stock Officer', 'permissions' => ['dashboard','purchase','stock','reports'], 'is_active' => true]
        );

        Role::updateOrCreate(
            ['code' => 'viewer'],
            ['name' => 'Report Viewer', 'permissions' => ['dashboard','reports'], 'is_active' => true]
        );

        User::updateOrCreate(
            ['email' => 'admin@pump.test'],
            ['name' => 'Admin User', 'password' => Hash::make('password'), 'role_id' => $adminRole->id, 'role' => 'admin', 'is_active' => true]
        );

        User::updateOrCreate(
            ['email' => 'cashier@pump.test'],
            ['name' => 'Cashier User', 'password' => Hash::make('password'), 'role_id' => $cashierRole->id, 'role' => 'cashier', 'is_active' => true]
        );

        User::updateOrCreate(
            ['email' => 'stock@pump.test'],
            ['name' => 'Stock User', 'password' => Hash::make('password'), 'role_id' => $stockRole->id, 'role' => 'stock_officer', 'is_active' => true]
        );

        foreach ([
            'company_name' => 'Petrol Pump MIS',
            'company_phone' => '+93 700 000 000',
            'company_address' => 'Afghanistan',
            'company_logo' => '',
            'invoice_header' => 'Fuel Sale Invoice',
            'invoice_footer' => 'Powered by OomClick Software Solution',
            'default_language' => 'ps',
            'base_currency' => 'AFN',
            'pos_easy_mode' => '1',
            'print_size' => 'small',
            'allow_negative_stock' => '0',
            'low_stock_sms' => '0',
            'receipt_note' => 'Thank you for your payment.',
            'sale_help_text' => 'Choose fuel, enter meter or quantity, then save and print.',
            'purchase_help_text' => 'Choose supplier, fuel, tank, quantity and price. Stock will increase automatically.',
        ] as $key => $value) {
            Setting::setValue($key, $value);
        }

        $afn = Currency::updateOrCreate(['code' => 'AFN'], ['name' => 'Afghani', 'symbol' => '؋', 'rate_to_base' => 1, 'is_base' => true, 'is_active' => true]);
        Currency::updateOrCreate(['code' => 'USD'], ['name' => 'US Dollar', 'symbol' => '$', 'rate_to_base' => 70, 'is_base' => false, 'is_active' => true]);
        Currency::updateOrCreate(['code' => 'PKR'], ['name' => 'Pakistani Rupee', 'symbol' => 'Rs', 'rate_to_base' => 0.25, 'is_base' => false, 'is_active' => true]);
        Currency::updateOrCreate(['code' => 'EUR'], ['name' => 'Euro', 'symbol' => '€', 'rate_to_base' => 78, 'is_base' => false, 'is_active' => true]);

        $petrol = FuelType::updateOrCreate(['code' => 'PETROL'], ['name_en' => 'Petrol', 'name_ps' => 'پټرول', 'name_dr' => 'پترول', 'unit' => 'liter', 'purchase_price' => 68, 'sale_price' => 72, 'min_stock_alert' => 500, 'color' => '#0ea5e9', 'is_active' => true]);
        $diesel = FuelType::updateOrCreate(['code' => 'DIESEL'], ['name_en' => 'Diesel', 'name_ps' => 'ډیزل', 'name_dr' => 'دیزل', 'unit' => 'liter', 'purchase_price' => 65, 'sale_price' => 70, 'min_stock_alert' => 500, 'color' => '#10b981', 'is_active' => true]);
        $gas = FuelType::updateOrCreate(['code' => 'GAS'], ['name_en' => 'Gas', 'name_ps' => 'ګیس', 'name_dr' => 'گاز', 'unit' => 'kg', 'purchase_price' => 55, 'sale_price' => 60, 'min_stock_alert' => 200, 'color' => '#f97316', 'is_active' => true]);

        foreach ([$petrol, $diesel, $gas] as $fuel) {
            FuelQuality::updateOrCreate(['fuel_type_id' => $fuel->id, 'name' => 'Normal'], ['test_result' => 'Accepted', 'is_active' => true]);
            FuelQuality::updateOrCreate(['fuel_type_id' => $fuel->id, 'name' => 'Premium'], ['test_result' => 'Accepted', 'is_active' => true]);
            Tank::updateOrCreate(['name' => $fuel->name_en.' Tank 1'], ['fuel_type_id' => $fuel->id, 'capacity' => 10000, 'current_qty' => 0, 'own_qty' => 0, 'customer_qty' => 0, 'location' => 'Main Pump', 'is_active' => true]);
        }

        Supplier::updateOrCreate(['name' => 'Default Supplier'], ['company_name' => 'Fuel Company', 'phone' => '0700000000', 'address' => 'Afghanistan', 'currency_id' => $afn->id]);
        Customer::updateOrCreate(['name' => 'Account Customer'], ['company_name' => 'Local Company', 'phone' => '0799000000', 'address' => 'Afghanistan', 'currency_id' => $afn->id, 'credit_limit' => 100000, 'is_active' => true]);

        foreach ([['Salary','معاش','معاش'], ['Electricity','برېښنا','برق'], ['Repair','ترمیم','ترمیم'], ['Fuel Transport','د تیلو انتقال','انتقال تیل'], ['Other','نور','دیگر']] as $cat) {
            ExpenseCategory::updateOrCreate(['name_en'=>$cat[0]], ['name_ps'=>$cat[1], 'name_dr'=>$cat[2]]);
        }

        Shift::updateOrCreate(['name' => 'Morning Shift', 'status' => 'open'], ['user_id' => 1, 'start_time' => now(), 'opening_cash' => 0]);
    }
}
