<?php

declare(strict_types=1);

namespace Dalten\RuianToUir;

/**
 * Item types produced by converters and used by formatters/writers.
 */
enum ItemType: string
{
    case Address = 'Address';
    case CadastralArea = 'CadastralArea';
    case CityPart = 'CityPart';
    case County = 'County';
    case Municipality = 'Municipality';
    case MunicipalityPart = 'MunicipalityPart';
    case Building = 'Building';
    case Plot = 'Plot';
    case PragueDistrict = 'PragueDistrict';
    case Region = 'Region';
    case Street = 'Street';

    public static function fromConverterName(string $name): self
    {
        try {
            return self::from($name);
        } catch (\ValueError) {
            throw new \InvalidArgumentException(\sprintf('Typ converteru "%s" není podporován SQL formatterem.', $name));
        }
    }

    public function tableName(): string
    {
        return match ($this) {
            self::Address => 'adresa',
            self::CityPart => 'mcast',
            self::County => 'kraj',
            self::Municipality => 'obec',
            self::MunicipalityPart => 'cobce',
            self::Building => 'objekt',
            self::PragueDistrict => 'pobvod',
            self::Region => 'okres',
            self::Street => 'ulice',
            self::Plot => 'parcela',
            self::CadastralArea => 'katastralni_uzemi',
        };
    }

    public function primaryKey(): string
    {
        return match ($this) {
            self::Address => 'adresa_kod',
            self::CadastralArea => 'ku_kod',
            self::CityPart => 'mcast_kod',
            self::County => 'kraj_kod',
            self::Municipality => 'obec_kod',
            self::MunicipalityPart => 'cobce_kod',
            self::Building => 'objekt_kod',
            self::Plot => 'parcela_kod',
            self::PragueDistrict => 'pobvod_kod',
            self::Region => 'okres_kod',
            self::Street => 'ulice_kod',
        };
    }
}
