<?php

declare(strict_types=1);

namespace App\Support;

class IslandGroupHelper
{
    public const LUZON = 'luzon';
    public const VISAYAS = 'visayas';
    public const MINDANAO = 'mindanao';

    /**
     * Map of region location_key (lowercased) → island group key.
     * Covers common name variants (e.g., "ncr" / "national capital region").
     */
    private const REGION_TO_ISLAND = [
        // Luzon
        'ncr' => self::LUZON,
        'national capital region' => self::LUZON,
        'metro manila' => self::LUZON,
        'car' => self::LUZON,
        'cordillera administrative region' => self::LUZON,
        'region i' => self::LUZON,
        'region 1' => self::LUZON,
        'ilocos region' => self::LUZON,
        'region ii' => self::LUZON,
        'region 2' => self::LUZON,
        'cagayan valley' => self::LUZON,
        'region iii' => self::LUZON,
        'region 3' => self::LUZON,
        'central luzon' => self::LUZON,
        'region iv-a' => self::LUZON,
        'region 4-a' => self::LUZON,
        'region iva' => self::LUZON,
        'calabarzon' => self::LUZON,
        'region iv-b' => self::LUZON,
        'region 4-b' => self::LUZON,
        'region ivb' => self::LUZON,
        'mimaropa' => self::LUZON,
        'mimaropa region' => self::LUZON,
        'region v' => self::LUZON,
        'region 5' => self::LUZON,
        'bicol region' => self::LUZON,

        // Visayas
        'region vi' => self::VISAYAS,
        'region 6' => self::VISAYAS,
        'western visayas' => self::VISAYAS,
        'region vii' => self::VISAYAS,
        'region 7' => self::VISAYAS,
        'central visayas' => self::VISAYAS,
        'region viii' => self::VISAYAS,
        'region 8' => self::VISAYAS,
        'eastern visayas' => self::VISAYAS,

        // Mindanao
        'region ix' => self::MINDANAO,
        'region 9' => self::MINDANAO,
        'zamboanga peninsula' => self::MINDANAO,
        'region x' => self::MINDANAO,
        'region 10' => self::MINDANAO,
        'northern mindanao' => self::MINDANAO,
        'region xi' => self::MINDANAO,
        'region 11' => self::MINDANAO,
        'davao region' => self::MINDANAO,
        'region xii' => self::MINDANAO,
        'region 12' => self::MINDANAO,
        'soccsksargen' => self::MINDANAO,
        'region xiii' => self::MINDANAO,
        'region 13' => self::MINDANAO,
        'caraga' => self::MINDANAO,
        'barmm' => self::MINDANAO,
        'bangsamoro' => self::MINDANAO,
        'bangsamoro autonomous region in muslim mindanao' => self::MINDANAO,
        'armm' => self::MINDANAO,
    ];

    private const ISLAND_LABELS = [
        self::LUZON => 'Luzon',
        self::VISAYAS => 'Visayas',
        self::MINDANAO => 'Mindanao',
    ];

    private const ISLAND_CENTERS = [
        self::LUZON => ['lat' => 16.5, 'lng' => 121.0],
        self::VISAYAS => ['lat' => 11.0, 'lng' => 123.5],
        self::MINDANAO => ['lat' => 7.5, 'lng' => 124.5],
    ];

    public static function islands(): array
    {
        return [self::LUZON, self::VISAYAS, self::MINDANAO];
    }

    public static function label(string $island): string
    {
        return self::ISLAND_LABELS[$island] ?? ucfirst($island);
    }

    public static function center(string $island): array
    {
        return self::ISLAND_CENTERS[$island] ?? ['lat' => 12.0, 'lng' => 122.5];
    }

    /**
     * Resolve an island group from a region's location_key or name.
     * Returns null if it can't be matched (caller should bucket as "unassigned").
     */
    public static function fromRegion(?string $regionKeyOrName): ?string
    {
        if (!$regionKeyOrName) return null;
        $key = strtolower(trim($regionKeyOrName));
        if (isset(self::REGION_TO_ISLAND[$key])) {
            return self::REGION_TO_ISLAND[$key];
        }
        // Loose substring match for entries like "Region IV-A (CALABARZON)"
        foreach (self::REGION_TO_ISLAND as $needle => $island) {
            if (str_contains($key, $needle)) return $island;
        }
        return null;
    }
}
