<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

/**
 * Seeds admin, test user, driver, one address, one order, services, providers, inventory, rescue services, shop categories/products, settings.
 * Run: php spark db:seed JamboSeeder
 *
 * Test credentials (phone = 9 digits as entered in app, PIN = 6 digits):
 * - Admin panel: 700000001 / 123456
 * - User app:    712345678 / 123456
 * - Driver app:  722222222 / 123456
 */
class JamboSeeder extends Seeder
{
    public function run()
    {
        $now = date('Y-m-d H:i:s');

        // Skip if admin already exists (safe re-run; services/providers already seeded)
        if ($this->db->table('users')->where('phone', '256700000001')->countAllResults() > 0) {
            return;
        }

        // Admin: phone 700000001 (9 digits) -> 256700000001, PIN 123456
        $this->db->table('users')->insert([
            'username'     => 'admin',
            'phone'        => '256700000001',
            'pin_hash'     => password_hash('123456', PASSWORD_DEFAULT),
            'role'         => 'admin',
            'jambo_status' => 'active',
            'active'       => 1,
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);
        // admin id not needed

        // Test user: phone 712345678, PIN 123456 (for user app login)
        $this->db->table('users')->insert([
            'username'     => 'user1',
            'phone'        => '256712345678',
            'pin_hash'     => password_hash('123456', PASSWORD_DEFAULT),
            'role'         => 'user',
            'jambo_status' => 'active',
            'active'       => 1,
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);
        $userId = $this->db->insertID();

        // Test driver: phone 722222222, PIN 123456 (for driver app login)
        $this->db->table('users')->insert([
            'username'     => 'driver1',
            'phone'        => '256722222222',
            'pin_hash'     => password_hash('123456', PASSWORD_DEFAULT),
            'role'         => 'driver',
            'jambo_status' => 'active',
            'active'       => 1,
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);
        $driverId = $this->db->insertID();

        // Saved address for test user
        $this->db->table('locations')->insert([
            'user_id'      => $userId,
            'latitude'     => 0.3476,
            'longitude'    => 32.5825,
            'address_name' => 'Kampala (test address)',
            'is_default'   => 1,
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);
        $locationId = $this->db->insertID();

        // Sample order (paid, assigned to driver - appears in user's "last 5" and driver's tasks)
        $this->db->table('orders')->insert([
            'user_id'      => $userId,
            'driver_id'    => $driverId,
            'location_id'  => $locationId,
            'order_type'   => 'gas',
            'order_meta'   => json_encode(['gas_size_id' => 5, 'refill_or_kit' => 'refill']),
            'total_amount' => 37000,
            'otp_code'     => '1234',
            'status'       => 'paid',
            'created_at'   => $now,
            'updated_at'   => $now,
        ]);

        $this->seedServicesProvidersOnly();
    }

    private function seedServicesProvidersOnly(): void
    {
        $now = date('Y-m-d H:i:s');
        $this->db->table('services')->insertBatch([
            ['name' => 'Rescue+', 'base_fee' => 5000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Gas', 'base_fee' => 3000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Fuel', 'base_fee' => 2000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
        ]);

        $this->db->table('providers')->insertBatch([
            ['name' => 'Shell', 'provider_type' => 'fuel', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Total', 'provider_type' => 'fuel', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Stabex', 'provider_type' => 'gas', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
        ]);

        $this->db->table('inventory')->insertBatch([
            ['provider_id' => 1, 'item_type' => 'fuel', 'item_name' => 'Petrol', 'price' => 5400, 'stock_count' => 100, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['provider_id' => 1, 'item_type' => 'fuel', 'item_name' => 'Diesel', 'price' => 5100, 'stock_count' => 100, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['provider_id' => 2, 'item_type' => 'fuel', 'item_name' => 'Petrol', 'price' => 5350, 'stock_count' => 100, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['provider_id' => 2, 'item_type' => 'fuel', 'item_name' => 'Diesel', 'price' => 5050, 'stock_count' => 100, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['provider_id' => 3, 'item_type' => 'gas_size', 'item_name' => '6kg', 'price' => 35000, 'stock_count' => 50, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['provider_id' => 3, 'item_type' => 'gas_size', 'item_name' => '13kg', 'price' => 65000, 'stock_count' => 50, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
        ]);

        $this->db->table('rescue_services')->insertBatch([
            ['name' => 'Flat tire', 'price' => 15000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Oil change', 'price' => 25000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
            ['name' => 'Battery jump', 'price' => 20000, 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')],
        ]);

        $this->db->table('shop_categories')->insertBatch([
            ['name' => 'Gas accessories', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'Car accessories', 'created_at' => $now, 'updated_at' => $now],
            ['name' => 'Lubricants', 'created_at' => $now, 'updated_at' => $now],
        ]);

        // Shop products (inventory with item_type=shop; category_id 1=Gas accessories, 2=Car, 3=Lubricants)
        if ($this->db->table('inventory')->where('item_type', 'shop')->countAllResults() === 0) {
            $this->db->table('inventory')->insertBatch([
                ['provider_id' => 1, 'category_id' => 1, 'item_type' => 'shop', 'item_name' => 'Gas hose', 'price' => 25000, 'stock_count' => 20, 'created_at' => $now, 'updated_at' => $now],
                ['provider_id' => 1, 'category_id' => 1, 'item_type' => 'shop', 'item_name' => 'Regulator', 'price' => 35000, 'stock_count' => 15, 'created_at' => $now, 'updated_at' => $now],
                ['provider_id' => 2, 'category_id' => 2, 'item_type' => 'shop', 'item_name' => 'Car charger', 'price' => 18000, 'stock_count' => 30, 'created_at' => $now, 'updated_at' => $now],
                ['provider_id' => 2, 'category_id' => 3, 'item_type' => 'shop', 'item_name' => 'Engine oil 1L', 'price' => 22000, 'stock_count' => 50, 'created_at' => $now, 'updated_at' => $now],
            ]);
        }

        $settingModel = model(\App\Models\SettingModel::class);
        $settingModel->setValue('service_fee', '2000');
        $settingModel->setValue('delivery_fee', '0');
        $settingModel->setValue('discord_webhook_url', '');
    }
}
