<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Tools\Context;

use App\Tools\Entity\EntityType;

/**
 * 已批准的当前页种类。每种 page_kind 只对应一个 EntityType，禁止客户端自造。
 */
enum PageKind: string
{
    case ProductView = 'product_view';

    case ProductEdit = 'product_edit';

    case CustomerEdit = 'customer_edit';

    case SupplierEdit = 'supplier_edit';

    case WarehouseEdit = 'warehouse_edit';

    case SalesOrderEdit = 'sales_order_edit';

    case PurchaseOrderEdit = 'purchase_order_edit';

    case ReceivableRecord = 'receivable_record';

    case PayableRecord = 'payable_record';

    public function entityType(): EntityType
    {
        return match ($this) {
            self::ProductView, self::ProductEdit => EntityType::Product,
            self::CustomerEdit => EntityType::Customer,
            self::SupplierEdit => EntityType::Supplier,
            self::WarehouseEdit => EntityType::Warehouse,
            self::SalesOrderEdit => EntityType::SalesOrder,
            self::PurchaseOrderEdit => EntityType::PurchaseOrder,
            self::ReceivableRecord => EntityType::Receivable,
            self::PayableRecord => EntityType::Payable,
        };
    }

    /**
     * @return list<string>
     */
    public static function values(): array
    {
        return array_map(
            static fn (self $kind): string => $kind->value,
            self::cases(),
        );
    }

    public static function tryFromMixed(mixed $value): ?self
    {
        if (! is_string($value) || trim($value) === '') {
            return null;
        }

        return self::tryFrom(trim($value));
    }
}
