<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class IndiaGujaratLocationsSeeder extends Seeder
{
    public function run()
    {
        $now = date('Y-m-d H:i:s');

        $countryId = $this->upsertCountry('India', 'IN', $now);
        $stateId = $this->upsertState($countryId, 'Gujarat', $now);

        $cities = [
            'Ahmedabad',
            'Surat',
            'Vadodara',
            'Rajkot',
            'Bhavnagar',
            'Jamnagar',
            'Junagadh',
            'Gandhinagar',
            'Anand',
            'Nadiad',
            'Mehsana',
            'Surendranagar',
            'Bharuch',
            'Navsari',
            'Valsad',
            'Vapi',
            'Porbandar',
            'Morbi',
            'Godhra',
            'Palanpur',
            'Patan',
            'Amreli',
            'Dahod',
            'Botad',
            'Gondal',
            'Veraval',
            'Somnath',
            'Dwarka',
            'Okha',
            'Jetpur',
            'Kalol',
            'Sanand',
            'Deesa',
            'Himatnagar',
            'Modasa',
            'Bhuj',
            'Anjar',
            'Mandvi',
            'Nakhatrana',
            'Kadi',
            'Unjha',
            'Visnagar',
            'Idar',
            'Chhota Udaipur',
            'Panchmahal',
            'Khambhat',
            'Dholka',
            'Lunawada',
            'Songadh',
            'Vyara',
        ];

        $inserted = 0;
        $existing = 0;

        foreach ($cities as $name) {
            $name = trim($name);
            if ($name === '') {
                continue;
            }

            $row = $this->db->table('rewarder_cities')
                ->where(['state_id' => $stateId, 'name' => $name])
                ->get()
                ->getRow();

            if ($row) {
                $existing++;
                continue;
            }

            $this->db->table('rewarder_cities')->insert([
                'state_id' => $stateId,
                'name' => $name,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
            $inserted++;
        }

        echo "Country ID: {$countryId}\n";
        echo "State ID: {$stateId}\n";
        echo "Cities inserted: {$inserted}\n";
        echo "Cities existing: {$existing}\n";
    }

    private function upsertCountry(string $name, ?string $code, string $now): int
    {
        $row = $this->db->table('rewarder_countries')
            ->where(['name' => $name])
            ->get()
            ->getRow();

        if ($row) {
            $this->db->table('rewarder_countries')->where('id', $row->id)->update([
                'code' => $code ?: null,
                'status' => 1,
                'updated_at' => $now,
            ]);
            return (int) $row->id;
        }

        $this->db->table('rewarder_countries')->insert([
            'name' => $name,
            'code' => $code ?: null,
            'status' => 1,
            'created_at' => $now,
            'updated_at' => $now,
        ]);
        return (int) $this->db->insertID();
    }

    private function upsertState(int $countryId, string $name, string $now): int
    {
        $row = $this->db->table('rewarder_states')
            ->where(['country_id' => $countryId, 'name' => $name])
            ->get()
            ->getRow();

        if ($row) {
            $this->db->table('rewarder_states')->where('id', $row->id)->update([
                'status' => 1,
                'updated_at' => $now,
            ]);
            return (int) $row->id;
        }

        $this->db->table('rewarder_states')->insert([
            'country_id' => $countryId,
            'name' => $name,
            'status' => 1,
            'created_at' => $now,
            'updated_at' => $now,
        ]);
        return (int) $this->db->insertID();
    }
}

