<?php

namespace App\Enums;

enum FieldType: string
{
    case VARCHAR = 'varchar';
    case TEXT = 'text';
    case LONGTEXT = 'longtext';
    case INT = 'int';
    case DECIMAL = 'decimal';
    case DATE = 'date';
    case DATETIME = 'datetime';
    case JSON = 'json';

    public function label(): string
    {
        return match ($this) {
            self::VARCHAR => '字符串',
            self::TEXT => '文本',
            self::LONGTEXT => '长文本',
            self::INT => '整数',
            self::DECIMAL => '小数',
            self::DATE => '日期',
            self::DATETIME => '日期时间',
            self::JSON => 'JSON',
        };
    }

    public function defaultLength(): ?int
    {
        return match ($this) {
            self::VARCHAR => 255,
            self::INT => 11,
            self::DECIMAL => 10,
            default => null,
        };
    }

    public function needsLength(): bool
    {
        return in_array($this, [self::VARCHAR, self::INT, self::DECIMAL]);
    }

    public function needsDecimalPrecision(): bool
    {
        return $this === self::DECIMAL;
    }
}
