<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Wallet;
use App\Models\BillService;
use App\Models\BillVariation;
use App\Models\Setting;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->createAdmin();
        $this->createDemoUser();
        $this->createBillServices();
        $this->createSettings();
    }

    protected function createAdmin(): void
    {
        $admin = User::updateOrCreate(
            ['email' => 'admin@tkbills.com'],
            [
                'first_name' => 'Admin',
                'last_name' => 'User',
                'phone' => '08012345678',
                'password' => Hash::make('password'),
                'role' => 'admin',
                'status' => 'active',
                'email_verified_at' => now(),
            ]
        );

        Wallet::firstOrCreate(['user_id' => $admin->id], ['user_id' => $admin->id]);
    }

    protected function createDemoUser(): void
    {
        $user = User::updateOrCreate(
            ['email' => 'user@tkbills.com'],
            [
                'first_name' => 'John',
                'last_name' => 'Doe',
                'phone' => '08087654321',
                'password' => Hash::make('password'),
                'role' => 'user',
                'status' => 'active',
                'email_verified_at' => now(),
            ]
        );

        Wallet::firstOrCreate(['user_id' => $user->id], ['user_id' => $user->id]);
    }

    protected function createBillServices(): void
    {
        $networks = [
            ['name' => 'MTN', 'slug' => 'mtn', 'code' => 'mtn', 'category' => 'airtime', 'service_charge' => 50, 'sort_order' => 1],
            ['name' => 'Airtel', 'slug' => 'airtel', 'code' => 'airtel', 'category' => 'airtime', 'service_charge' => 50, 'sort_order' => 2],
            ['name' => 'Glo', 'slug' => 'glo', 'code' => 'glo', 'category' => 'airtime', 'service_charge' => 50, 'sort_order' => 3],
            ['name' => '9mobile', 'slug' => '9mobile', 'code' => '9mobile', 'category' => 'airtime', 'service_charge' => 50, 'sort_order' => 4],
        ];

        foreach ($networks as $network) {
            BillService::updateOrCreate(['slug' => $network['slug']], $network);
        }

        $dataNetworks = [
            ['name' => 'MTN Data', 'slug' => 'mtn-data', 'code' => 'mtn-data', 'category' => 'data', 'service_charge' => 50, 'sort_order' => 1],
            ['name' => 'Airtel Data', 'slug' => 'airtel-data', 'code' => 'airtel-data', 'category' => 'data', 'service_charge' => 50, 'sort_order' => 2],
            ['name' => 'Glo Data', 'slug' => 'glo-data', 'code' => 'glo-data', 'category' => 'data', 'service_charge' => 50, 'sort_order' => 3],
            ['name' => '9mobile Data', 'slug' => '9mobile-data', 'code' => '9mobile-data', 'category' => 'data', 'service_charge' => 50, 'sort_order' => 4],
        ];

        foreach ($dataNetworks as $network) {
            BillService::updateOrCreate(['slug' => $network['slug']], $network);
        }

        $cableProviders = [
            ['name' => 'DSTV', 'slug' => 'dstv', 'code' => 'dstv', 'category' => 'cable', 'service_charge' => 200, 'sort_order' => 1],
            ['name' => 'GOtv', 'slug' => 'gotv', 'code' => 'gotv', 'category' => 'cable', 'service_charge' => 200, 'sort_order' => 2],
            ['name' => 'Startimes', 'slug' => 'startimes', 'code' => 'startimes', 'category' => 'cable', 'service_charge' => 200, 'sort_order' => 3],
        ];

        foreach ($cableProviders as $provider) {
            BillService::updateOrCreate(['slug' => $provider['slug']], $provider);
        }

        $electricityProviders = [
            ['name' => 'Ikeja Electric', 'slug' => 'ikeja-electric', 'code' => 'ikeja-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 1],
            ['name' => 'Eko Electricity', 'slug' => 'eko-electric', 'code' => 'eko-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 2],
            ['name' => 'Abuja Electricity', 'slug' => 'abuja-electric', 'code' => 'abuja-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 3],
            ['name' => 'Ibadan Electricity', 'slug' => 'ibadan-electric', 'code' => 'ibadan-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 4],
            ['name' => 'Port Harcourt Electric', 'slug' => 'ph-electric', 'code' => 'ph-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 5],
            ['name' => 'Kano Electricity', 'slug' => 'kano-electric', 'code' => 'kano-electric', 'category' => 'electricity', 'service_charge' => 100, 'sort_order' => 6],
        ];

        foreach ($electricityProviders as $provider) {
            BillService::updateOrCreate(['slug' => $provider['slug']], $provider);
        }

        $examTypes = [
            ['name' => 'WAEC', 'slug' => 'waec', 'code' => 'waec', 'category' => 'exam_pin', 'service_charge' => 0, 'sort_order' => 1],
            ['name' => 'NECO', 'slug' => 'neco', 'code' => 'neco', 'category' => 'exam_pin', 'service_charge' => 0, 'sort_order' => 2],
            ['name' => 'JAMB', 'slug' => 'jamb', 'code' => 'jamb', 'category' => 'exam_pin', 'service_charge' => 0, 'sort_order' => 3],
            ['name' => 'NBAIS', 'slug' => 'nbais', 'code' => 'nbais', 'category' => 'exam_pin', 'service_charge' => 0, 'sort_order' => 4],
        ];

        foreach ($examTypes as $exam) {
            BillService::updateOrCreate(['slug' => $exam['slug']], $exam);
        }
    }

    protected function createSettings(): void
    {
        $settings = [
            ['group' => 'billing', 'key' => 'default_service_charge', 'value' => '50', 'type' => 'float', 'description' => 'Default service charge amount'],
            ['group' => 'billing', 'key' => 'service_charge_percentage', 'value' => '1.5', 'type' => 'float', 'description' => 'Service charge percentage'],
            ['group' => 'billing', 'key' => 'min_funding', 'value' => '100', 'type' => 'integer', 'description' => 'Minimum wallet funding amount'],
            ['group' => 'billing', 'key' => 'max_funding', 'value' => '500000', 'type' => 'integer', 'description' => 'Maximum wallet funding amount'],
            ['group' => 'billing', 'key' => 'max_daily_transactions', 'value' => '50', 'type' => 'integer', 'description' => 'Maximum daily transactions per user'],
            ['group' => 'general', 'key' => 'maintenance_mode', 'value' => '0', 'type' => 'boolean', 'description' => 'Enable/disable maintenance mode'],
            ['group' => 'general', 'key' => 'platform_name', 'value' => 'Clikzpay', 'type' => 'string', 'description' => 'Platform name'],
        ];

        foreach ($settings as $setting) {
            Setting::updateOrCreate(['key' => $setting['key']], $setting);
        }
    }
}
