<?php

namespace Database\Seeders;

use App\Models\City;
use App\Models\State;
use App\Models\Zone;
use App\Models\ZoneRule;
use Illuminate\Database\Seeder;

/**
 * Maharashtra (DCPR 2034) seed data.
 * Rules sourced from: Maharashtra Unified Development Control & Promotion Regulations 2034 (DCPR 2034).
 */
class MaharashtraDataSeeder extends Seeder
{
    public function run(): void
    {
        $mh = State::where('code', 'MH')->first();
        if (!$mh) {
            $this->command->warn('Maharashtra state not found. Run IndiaStatesSeeder first.');
            return;
        }

        // ── Cities ────────────────────────────────────────────────────────
        $cities = [
            ['name' => 'Mumbai',        'code' => 'MUM', 'planning_authority' => 'MCGM',         'dcr_version' => 'DCPR 2034', 'master_plan_year' => 2034],
            ['name' => 'Pune',          'code' => 'PUN', 'planning_authority' => 'PMC / PCMC',   'dcr_version' => 'PDCR 2017', 'master_plan_year' => 2041],
            ['name' => 'Nagpur',        'code' => 'NAG', 'planning_authority' => 'NMC / NUDA',   'dcr_version' => 'NDCR 2019', 'master_plan_year' => 2041],
            ['name' => 'Nashik',        'code' => 'NSK', 'planning_authority' => 'NMC Nashik',   'dcr_version' => 'DCR 2018',  'master_plan_year' => 2031],
            ['name' => 'Aurangabad',    'code' => 'AUR', 'planning_authority' => 'AMC',           'dcr_version' => 'DCR 2015',  'master_plan_year' => 2031],
            ['name' => 'Navi Mumbai',   'code' => 'NMM', 'planning_authority' => 'CIDCO / NMMC', 'dcr_version' => 'DCPR 2034', 'master_plan_year' => 2031],
            ['name' => 'Thane',         'code' => 'THA', 'planning_authority' => 'TMC',           'dcr_version' => 'DCPR 2034', 'master_plan_year' => 2031],
            ['name' => 'Kalyan',        'code' => 'KAL', 'planning_authority' => 'KDMC',          'dcr_version' => 'DCPR 2034', 'master_plan_year' => 2031],
        ];

        $cityModels = [];
        foreach ($cities as $c) {
            $city = City::firstOrCreate(
                ['state_id' => $mh->id, 'name' => $c['name']],
                array_merge($c, ['state_id' => $mh->id, 'is_active' => true])
            );
            $cityModels[$c['name']] = $city;
        }

        $mumbai = $cityModels['Mumbai'];
        $pune   = $cityModels['Pune'];

        // ── Zones ─────────────────────────────────────────────────────────
        $zoneData = [
            [
                'name'                => 'R1 — Residential (Low Density)',
                'zone_type'           => 'Residential',
                'far_fsi'             => 1.0,
                'max_height_m'        => 11.0,
                'max_ground_coverage' => 50.0,
                'front_setback_m'     => 3.0,
                'side_setback_m'      => 1.5,
                'rear_setback_m'      => 3.0,
                'open_space_pct'      => 10.0,
                'permitted_uses'      => 'Residential dwellings, Schools, Clinics, Places of worship',
                'regulation_ref'      => 'DCPR 2034 — Regulation 30',
                'description'         => 'Low-density residential zone, typically in suburbs and older residential pockets.',
            ],
            [
                'name'                => 'R2 — Residential (Medium Density)',
                'zone_type'           => 'Residential',
                'far_fsi'             => 1.5,
                'max_height_m'        => 20.0,
                'max_ground_coverage' => 45.0,
                'front_setback_m'     => 4.5,
                'side_setback_m'      => 2.0,
                'rear_setback_m'      => 3.0,
                'open_space_pct'      => 15.0,
                'permitted_uses'      => 'Residential, Schools, Hospitals (upto 1000 sqm), Offices (ground floor)',
                'regulation_ref'      => 'DCPR 2034 — Regulation 31',
                'description'         => 'Medium-density residential zone covering most Mumbai suburbs.',
            ],
            [
                'name'                => 'C1 — Commercial (Neighbourhood)',
                'zone_type'           => 'Commercial',
                'far_fsi'             => 2.0,
                'max_height_m'        => 25.0,
                'max_ground_coverage' => 50.0,
                'front_setback_m'     => 4.5,
                'side_setback_m'      => 2.5,
                'rear_setback_m'      => 3.0,
                'open_space_pct'      => 10.0,
                'permitted_uses'      => 'Retail, Offices, Hotels, Banks, Restaurants, Commercial complexes',
                'regulation_ref'      => 'DCPR 2034 — Regulation 32',
                'description'         => 'Neighbourhood-level commercial zone.',
            ],
            [
                'name'                => 'C2 — Commercial (District Level)',
                'zone_type'           => 'Commercial',
                'far_fsi'             => 3.0,
                'max_height_m'        => 45.0,
                'max_ground_coverage' => 55.0,
                'front_setback_m'     => 6.0,
                'side_setback_m'      => 3.0,
                'rear_setback_m'      => 4.5,
                'open_space_pct'      => 10.0,
                'permitted_uses'      => 'Large retail, Malls, IT Parks, Hotels, Mixed-use commercial',
                'regulation_ref'      => 'DCPR 2034 — Regulation 33',
                'description'         => 'District-level commercial zone for high-intensity business use.',
            ],
            [
                'name'                => 'IT — IT / ITES Zone',
                'zone_type'           => 'Industrial',
                'far_fsi'             => 3.0,
                'max_height_m'        => 50.0,
                'max_ground_coverage' => 40.0,
                'front_setback_m'     => 6.0,
                'side_setback_m'      => 3.0,
                'rear_setback_m'      => 4.5,
                'open_space_pct'      => 20.0,
                'permitted_uses'      => 'IT/ITES offices, Data centres, BPO, Hostels for IT staff',
                'regulation_ref'      => 'DCPR 2034 — Regulation 58',
                'description'         => 'Dedicated IT/ITES zone with enhanced FSI incentives.',
            ],
            [
                'name'                => 'MIX — Mixed Use',
                'zone_type'           => 'Mixed',
                'far_fsi'             => 2.5,
                'max_height_m'        => 35.0,
                'max_ground_coverage' => 50.0,
                'front_setback_m'     => 4.5,
                'side_setback_m'      => 2.5,
                'rear_setback_m'      => 3.0,
                'open_space_pct'      => 15.0,
                'permitted_uses'      => 'Residential, Retail (Ground), Offices, Hotels, Service apartments',
                'regulation_ref'      => 'DCPR 2034 — Regulation 35',
                'description'         => 'Mixed-use zone along main roads and transit corridors.',
            ],
            [
                'name'                => 'GB — Green Belt',
                'zone_type'           => 'Green Belt',
                'far_fsi'             => 0.1,
                'max_height_m'        => 6.0,
                'max_ground_coverage' => 5.0,
                'front_setback_m'     => 6.0,
                'side_setback_m'      => 3.0,
                'rear_setback_m'      => 6.0,
                'open_space_pct'      => 85.0,
                'permitted_uses'      => 'Parks, Playgrounds, Open spaces, Agriculture',
                'regulation_ref'      => 'DCPR 2034 — Regulation 45',
                'description'         => 'Protected green belt — no permanent construction except recreational structures.',
            ],
        ];

        $zoneModels = [];
        foreach ($zoneData as $z) {
            $zone = Zone::firstOrCreate(
                ['state_id' => $mh->id, 'name' => $z['name']],
                array_merge($z, ['state_id' => $mh->id, 'is_active' => true])
            );
            $zoneModels[$z['name']] = $zone;
        }

        // ── Zone Rules (DCPR 2034 — FSI by road width) ────────────────────
        // Source: DCPR 2034 Regulation 30-33, Table of FSI by road width

        $this->seedR1Rules($zoneModels['R1 — Residential (Low Density)'], $mumbai);
        $this->seedR2Rules($zoneModels['R2 — Residential (Medium Density)'], $mumbai);
        $this->seedC1Rules($zoneModels['C1 — Commercial (Neighbourhood)'], $mumbai);
        $this->seedC2Rules($zoneModels['C2 — Commercial (District Level)'], $mumbai);
        $this->seedMixRules($zoneModels['MIX — Mixed Use'], $mumbai);

        $this->command->info('Maharashtra data seeded successfully.');
    }

    private function seedR1Rules(Zone $zone, City $city): void
    {
        $rules = [
            // road_width_min, road_width_max, max_far, max_height, coverage, open_space, front, side, rear, parking, citation
            [0,     6,    1.0, 11.0, 50.0, 10.0, 3.0, 1.5, 3.0, 0.5, 'DCPR 2034 Reg.30(1) — R1, road < 6m'],
            [6,     9,    1.0, 14.0, 50.0, 10.0, 3.0, 1.5, 3.0, 0.5, 'DCPR 2034 Reg.30(2) — R1, 6-9m road'],
            [9,     12,   1.0, 14.0, 50.0, 10.0, 3.0, 1.5, 3.0, 0.5, 'DCPR 2034 Reg.30(3) — R1, 9-12m road'],
            [12,    null, 1.0, 15.0, 50.0, 10.0, 3.0, 1.5, 3.0, 0.5, 'DCPR 2034 Reg.30(4) — R1, ≥12m road'],
        ];

        foreach ($rules as $r) {
            ZoneRule::firstOrCreate(
                ['zone_id' => $zone->id, 'city_id' => $city->id, 'road_width_min_m' => $r[0]],
                [
                    'road_width_max_m'    => $r[1],
                    'max_far'             => $r[2],
                    'max_height_m'        => $r[3],
                    'max_ground_coverage' => $r[4],
                    'open_space_pct'      => $r[5],
                    'front_setback_m'     => $r[6],
                    'side_setback_m'      => $r[7],
                    'rear_setback_m'      => $r[8],
                    'parking_ratio_ecs'   => $r[9],
                    'rule_source'         => $r[10],
                    'is_active'           => true,
                ]
            );
        }
    }

    private function seedR2Rules(Zone $zone, City $city): void
    {
        $rules = [
            [0,     6,    1.0, 14.0, 45.0, 15.0, 3.0, 2.0, 3.0, 0.5, 'DCPR 2034 Reg.31(1) — R2, road < 6m'],
            [6,     9,    1.5, 20.0, 45.0, 15.0, 4.5, 2.0, 3.0, 0.5, 'DCPR 2034 Reg.31(2) — R2, 6-9m road'],
            [9,     12,   2.0, 24.0, 45.0, 15.0, 4.5, 2.0, 3.0, 0.5, 'DCPR 2034 Reg.31(3) — R2, 9-12m road'],
            [12,    18,   2.5, 30.0, 45.0, 15.0, 6.0, 3.0, 4.5, 0.5, 'DCPR 2034 Reg.31(4) — R2, 12-18m road'],
            [18,    null, 3.0, 36.0, 45.0, 15.0, 6.0, 3.0, 4.5, 0.5, 'DCPR 2034 Reg.31(5) — R2, ≥18m road'],
        ];

        foreach ($rules as $r) {
            ZoneRule::firstOrCreate(
                ['zone_id' => $zone->id, 'city_id' => $city->id, 'road_width_min_m' => $r[0]],
                [
                    'road_width_max_m'    => $r[1],
                    'max_far'             => $r[2],
                    'max_height_m'        => $r[3],
                    'max_ground_coverage' => $r[4],
                    'open_space_pct'      => $r[5],
                    'front_setback_m'     => $r[6],
                    'side_setback_m'      => $r[7],
                    'rear_setback_m'      => $r[8],
                    'parking_ratio_ecs'   => $r[9],
                    'rule_source'         => $r[10],
                    'is_active'           => true,
                ]
            );
        }
    }

    private function seedC1Rules(Zone $zone, City $city): void
    {
        $rules = [
            [0,     9,    1.5, 20.0, 50.0, 10.0, 4.5, 2.5, 3.0, 1.0, 'DCPR 2034 Reg.32(1) — C1, road < 9m'],
            [9,     12,   2.0, 25.0, 50.0, 10.0, 4.5, 2.5, 3.0, 1.0, 'DCPR 2034 Reg.32(2) — C1, 9-12m'],
            [12,    18,   2.5, 30.0, 50.0, 10.0, 4.5, 2.5, 3.0, 1.0, 'DCPR 2034 Reg.32(3) — C1, 12-18m'],
            [18,    null, 3.0, 35.0, 50.0, 10.0, 6.0, 3.0, 4.5, 1.0, 'DCPR 2034 Reg.32(4) — C1, ≥18m'],
        ];

        foreach ($rules as $r) {
            ZoneRule::firstOrCreate(
                ['zone_id' => $zone->id, 'city_id' => $city->id, 'road_width_min_m' => $r[0]],
                [
                    'road_width_max_m'    => $r[1],
                    'max_far'             => $r[2],
                    'max_height_m'        => $r[3],
                    'max_ground_coverage' => $r[4],
                    'open_space_pct'      => $r[5],
                    'front_setback_m'     => $r[6],
                    'side_setback_m'      => $r[7],
                    'rear_setback_m'      => $r[8],
                    'parking_ratio_ecs'   => $r[9],
                    'rule_source'         => $r[10],
                    'is_active'           => true,
                ]
            );
        }
    }

    private function seedC2Rules(Zone $zone, City $city): void
    {
        $rules = [
            [12,    18,   2.5, 35.0, 55.0, 10.0, 6.0, 3.0, 4.5, 1.5, 'DCPR 2034 Reg.33(1) — C2, 12-18m'],
            [18,    24,   3.0, 45.0, 55.0, 10.0, 6.0, 3.0, 4.5, 1.5, 'DCPR 2034 Reg.33(2) — C2, 18-24m'],
            [24,    null, 4.0, 60.0, 55.0, 10.0, 6.0, 3.0, 4.5, 1.5, 'DCPR 2034 Reg.33(3) — C2, ≥24m'],
        ];

        foreach ($rules as $r) {
            ZoneRule::firstOrCreate(
                ['zone_id' => $zone->id, 'city_id' => $city->id, 'road_width_min_m' => $r[0]],
                [
                    'road_width_max_m'    => $r[1],
                    'max_far'             => $r[2],
                    'max_height_m'        => $r[3],
                    'max_ground_coverage' => $r[4],
                    'open_space_pct'      => $r[5],
                    'front_setback_m'     => $r[6],
                    'side_setback_m'      => $r[7],
                    'rear_setback_m'      => $r[8],
                    'parking_ratio_ecs'   => $r[9],
                    'rule_source'         => $r[10],
                    'is_active'           => true,
                ]
            );
        }
    }

    private function seedMixRules(Zone $zone, City $city): void
    {
        $rules = [
            [9,     12,   2.0, 30.0, 50.0, 15.0, 4.5, 2.5, 3.0, 1.0, 'DCPR 2034 Reg.35(1) — MIX, 9-12m'],
            [12,    18,   2.5, 35.0, 50.0, 15.0, 6.0, 3.0, 4.5, 1.0, 'DCPR 2034 Reg.35(2) — MIX, 12-18m'],
            [18,    null, 3.0, 45.0, 50.0, 15.0, 6.0, 3.0, 4.5, 1.0, 'DCPR 2034 Reg.35(3) — MIX, ≥18m'],
        ];

        foreach ($rules as $r) {
            ZoneRule::firstOrCreate(
                ['zone_id' => $zone->id, 'city_id' => $city->id, 'road_width_min_m' => $r[0]],
                [
                    'road_width_max_m'    => $r[1],
                    'max_far'             => $r[2],
                    'max_height_m'        => $r[3],
                    'max_ground_coverage' => $r[4],
                    'open_space_pct'      => $r[5],
                    'front_setback_m'     => $r[6],
                    'side_setback_m'      => $r[7],
                    'rear_setback_m'      => $r[8],
                    'parking_ratio_ecs'   => $r[9],
                    'rule_source'         => $r[10],
                    'is_active'           => true,
                ]
            );
        }
    }
}
