<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use App\Models\Branch;
use App\Enums\RoomType;
use App\Enums\RoomStatus;
use App\Http\Responses\ApiResponse;
/**
 * @OA\Tag(
 *     name="Branch",
 *     description="分店包廂"
 * )
 */
class BranchControlController extends Controller
{
    /**
     * @OA\Get(
     *     path="/api/branches",
     *     tags={"Branch"},
     *     summary="取得分店資訊",
     *     description="需帶入 Bearer Token 取得當前用戶可存取的分店資訊",
     *     operationId="Branches",
     *     security={{"Authorization":{}}},
     *     @OA\Response(
     *         response=200,
     *         description="成功回傳分店列表",
     *         @OA\JsonContent(
     *             @OA\Property(property="branches", type="array", @OA\Items(
     *                 @OA\Property(property="id", type="integer", example=1),
     *                 @OA\Property(property="branch_name", type="string", example="測試"),
     *                 @OA\Property(property="enable", type="boolean", example=true),
     *                 @OA\Property(
     *                     property="rooms",
     *                     type="object",
     *                     additionalProperties={"$ref": "#/components/schemas/RoomInfo"}
     *                 )
     *             ))
     *         )
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="Unauthorized",
     *         @OA\JsonContent(
     *             allOf={
     *                 @OA\Schema(ref="#/components/schemas/ApiResponse"),
     *                 @OA\Schema(
     *                     @OA\Property(property="code", type="string", example="UNAUTHORIZED"),
     *                     @OA\Property(property="message", type="string", example="Unauthorized"),
     *                     @OA\Property(property="data", type="null")
     *                 )
     *             }
     *         )
     *     ),
     * )
     * @OA\Schema(
     *     schema="RoomInfo",
     *     type="object",
     *     @OA\Property(property="is_online", type="boolean", example=true),
     *     @OA\Property(property="status", ref="#/components/schemas/RoomStatus"),
     *     @OA\Property(property="started_at", type="string", format="date-time", example="2025-06-04T10:00:00Z"),
     *     @OA\Property(property="ended_at", type="string", format="date-time", example="2025-06-04T12:00:00Z")
     * )
     */
    public function Branches(Request $request): JsonResponse
    {
        $branches = Branch::with(['rooms' => function ($query) {
            $query->orderBy('name', 'asc'); // 對 rooms 的 name 排序
        }])->get()->map(function ($branch) {
            return [
                'id' => $branch->id,
                'branch_name' => $branch->name,
                'enable' => $branch->enable,
                'rooms' => $branch->rooms
                ->filter(fn($room) => $room->type === RoomType::PC)
                ->mapWithKeys(function ($room) {
                    $roomName = ($room->type?->value ?? '') . $room->name;
                    return [
                        $roomName => [
                            'is_online' => $room->is_online,
                            'status' => $room->status?->value ?? null,
                            'started_at' => $room->started_at ? $room->started_at->timezone('Asia/Taipei')->format('Y-m-d H:i:s') : null,
                            'ended_at'   => $room->ended_at ? $room->ended_at->timezone('Asia/Taipei')->format('Y-m-d H:i:s') : null
                        ]
                    ];
                }),
            ];
        });
        return ApiResponse::success(['branches' => $branches]);
    }
}