<?php

/**
 * 文件功能：聊天室当日状态目录
 * 负责集中维护聊天室状态分组、文案、图标与激活状态解析逻辑。
 */

namespace App\Support;

use Carbon\Carbon;
use Carbon\CarbonInterface;

class ChatDailyStatusCatalog
{
    /**
     * 固定状态目录。
     *
     * @return array<int, array{group: string, items: array<int, array{key: string, label: string, icon: string}>}>
     */
    public static function groupedOptions(): array
    {
        return [
            [
                'group' => '心情想法',
                'items' => [
                    ['key' => 'feeling_great', 'label' => '美滋滋', 'icon' => '🙂'],
                    ['key' => 'heartbroken', 'label' => '裂开', 'icon' => '💔'],
                    ['key' => 'lucky_fish', 'label' => '求锦鲤', 'icon' => '🐟'],
                    ['key' => 'waiting_sun', 'label' => '等天晴', 'icon' => '☀️'],
                    ['key' => 'tired', 'label' => '疲惫', 'icon' => '😮‍💨'],
                    ['key' => 'dazed', 'label' => '发呆', 'icon' => '💭'],
                    ['key' => 'charging', 'label' => '冲', 'icon' => '🚀'],
                    ['key' => 'emo', 'label' => 'emo', 'icon' => '🥀'],
                    ['key' => 'overthinking', 'label' => '胡思乱想', 'icon' => '☁️'],
                    ['key' => 'energetic', 'label' => '元气满满', 'icon' => '✨'],
                    ['key' => 'bot', 'label' => 'bot', 'icon' => '🤖'],
                ],
            ],
            [
                'group' => '工作学习',
                'items' => [
                    ['key' => 'working_hard', 'label' => '搬砖', 'icon' => '🧱'],
                    ['key' => 'studying', 'label' => '沉迷学习', 'icon' => '📚'],
                    ['key' => 'busy', 'label' => '忙', 'icon' => '🙆'],
                    ['key' => 'slacking', 'label' => '摸鱼', 'icon' => '🐟'],
                    ['key' => 'business_trip', 'label' => '出差', 'icon' => '✈️'],
                    ['key' => 'running_home', 'label' => '飞奔回家', 'icon' => '🏃'],
                    ['key' => 'do_not_disturb', 'label' => '勿扰模式', 'icon' => '🌙'],
                ],
            ],
            [
                'group' => '活动',
                'items' => [
                    ['key' => 'wandering', 'label' => '浪', 'icon' => '🌊'],
                    ['key' => 'clocking_in', 'label' => '打卡', 'icon' => '✌️'],
                    ['key' => 'exercising', 'label' => '运动', 'icon' => '🏃‍♂️'],
                    ['key' => 'coffee', 'label' => '喝咖啡', 'icon' => '☕'],
                    ['key' => 'milk_tea', 'label' => '喝奶茶', 'icon' => '🧋'],
                    ['key' => 'eating', 'label' => '干饭', 'icon' => '🍚'],
                    ['key' => 'parenting', 'label' => '带娃', 'icon' => '🧒'],
                    ['key' => 'save_world', 'label' => '拯救世界', 'icon' => '🦸'],
                    ['key' => 'selfie', 'label' => '自拍', 'icon' => '🤳'],
                ],
            ],
            [
                'group' => '休息',
                'items' => [
                    ['key' => 'retreat', 'label' => '闭关', 'icon' => '🧘'],
                    ['key' => 'staying_home', 'label' => '宅', 'icon' => '🛋️'],
                    ['key' => 'sleeping', 'label' => '睡觉', 'icon' => '💤'],
                    ['key' => 'cat_time', 'label' => '吸猫', 'icon' => '🐈'],
                    ['key' => 'walk_dog', 'label' => '遛狗', 'icon' => '🐕'],
                    ['key' => 'gaming', 'label' => '玩游戏', 'icon' => '🎮'],
                    ['key' => 'listening_music', 'label' => '听歌', 'icon' => '🎧'],
                ],
            ],
        ];
    }

    /**
     * 返回全部合法状态键。
     *
     * @return array<int, string>
     */
    public static function keys(): array
    {
        return array_values(array_map(
            static fn (array $item): string => $item['key'],
            self::flatOptions()
        ));
    }

    /**
     * 根据状态键查找对应配置。
     *
     * @return array{key: string, label: string, icon: string, group: string}|null
     */
    public static function find(?string $key): ?array
    {
        if ($key === null || $key === '') {
            return null;
        }

        foreach (self::flatOptions() as $item) {
            if ($item['key'] === $key) {
                return $item;
            }
        }

        return null;
    }

    /**
     * 解析当前仍处于有效期内的状态数据。
     *
     * @param  CarbonInterface|string|null  $expiresAt  到期时间
     * @return array{key: string, label: string, icon: string, group: string, expires_at: string}|null
     */
    public static function resolveActiveStatus(?string $key, CarbonInterface|string|null $expiresAt): ?array
    {
        $option = self::find($key);
        if ($option === null) {
            return null;
        }

        $expiresAtCarbon = self::normalizeExpiresAt($expiresAt);
        if ($expiresAtCarbon === null || $expiresAtCarbon->isPast()) {
            return null;
        }

        return [
            'key' => $option['key'],
            'label' => $option['label'],
            'icon' => $option['icon'],
            'group' => $option['group'],
            'expires_at' => $expiresAtCarbon->toIso8601String(),
        ];
    }

    /**
     * 将分组目录整理为扁平列表，便于校验与查找。
     *
     * @return array<int, array{key: string, label: string, icon: string, group: string}>
     */
    private static function flatOptions(): array
    {
        $flat = [];

        foreach (self::groupedOptions() as $group) {
            foreach ($group['items'] as $item) {
                $flat[] = [
                    'key' => $item['key'],
                    'label' => $item['label'],
                    'icon' => $item['icon'],
                    'group' => $group['group'],
                ];
            }
        }

        return $flat;
    }

    /**
     * 规整状态到期时间为 Carbon 对象。
     *
     * @param  CarbonInterface|string|null  $expiresAt  原始到期时间
     */
    private static function normalizeExpiresAt(CarbonInterface|string|null $expiresAt): ?CarbonInterface
    {
        if ($expiresAt instanceof CarbonInterface) {
            return $expiresAt;
        }

        if ($expiresAt === null || $expiresAt === '') {
            return null;
        }

        return Carbon::parse($expiresAt);
    }
}
