package com.zioinfo.kintex.dashboard;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 주최자 대시보드 집계 매퍼 — event·booth·design_plan·utility_order·render_job read-only.
 * booth 는 {@code layout.event_id} 경유로 행사에 귀속된다. 마이그레이션 불변.
 */
@Mapper
public interface DashboardMapper {

    /** 행사 헤더(마일스톤 파생용 시작/종료일). 없으면 null. */
    @Select("""
            SELECT to_char(start_date, 'YYYY-MM-DD') AS "startDate",
                   to_char(end_date,   'YYYY-MM-DD') AS "endDate",
                   name
            FROM event WHERE id = #{eventId}
            """)
    Map<String, Object> findEventHeader(@Param("eventId") String eventId);

    /** KPI 원자 집계(단일 행) — 부스 수·목표 부스·설계 승인·유틸리티 신청·렌더 완료. */
    @Select("""
            SELECT
              (SELECT count(*) FROM booth b JOIN layout l ON l.id = b.layout_id
                 WHERE l.event_id = #{eventId}) AS "boothCount",
              (SELECT COALESCE(sum(h.booth_capacity), 0) FROM hall_assignment ha
                 JOIN hall h ON h.id = ha.hall_id WHERE ha.event_id = #{eventId}) AS "targetBooths",
              (SELECT count(*) FROM booth b JOIN layout l ON l.id = b.layout_id
                 JOIN design_plan dp ON dp.booth_id = b.id
                 WHERE l.event_id = #{eventId} AND dp.status = 'approved') AS "designApproved",
              (SELECT count(*) FROM design_plan dp JOIN booth b ON b.id = dp.booth_id
                 JOIN layout l ON l.id = b.layout_id WHERE l.event_id = #{eventId}) AS "designTotal",
              (SELECT count(*) FROM utility_order u WHERE u.event_id = #{eventId}) AS "utilityOrders",
              (SELECT count(*) FROM render_job r WHERE r.event_id = #{eventId} AND r.status = 'DONE') AS "renderDone"
            """)
    Map<String, Object> findKpiCounts(@Param("eventId") String eventId);

    /** 참가업체 부스 현황(최신 설계안 상태 조인). 최대 100건. */
    @Select("""
            SELECT b.booth_no AS "boothNo",
                   b.assigned_company_name AS "companyName",
                   COALESCE(dp.status, 'draft') AS status
            FROM booth b
            JOIN layout l ON l.id = b.layout_id
            LEFT JOIN LATERAL (
                SELECT status FROM design_plan d
                WHERE d.booth_id = b.id ORDER BY version DESC LIMIT 1
            ) dp ON true
            WHERE l.event_id = #{eventId}
            ORDER BY b.booth_no NULLS LAST
            LIMIT 100
            """)
    List<Map<String, Object>> findExhibitors(@Param("eventId") String eventId);

    /** 활동 피드 — 렌더잡 최근 이력(승인/위반 원천 부재 시 render 만). 최대 12건. */
    @Select("""
            SELECT r.id,
                   r.status,
                   r.shot_preset AS "shotPreset",
                   r.booth_id AS "boothId",
                   to_char(r.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') AS at
            FROM render_job r
            WHERE r.event_id = #{eventId}
            ORDER BY r.updated_at DESC
            LIMIT 12
            """)
    List<Map<String, Object>> findRecentRenders(@Param("eventId") String eventId);
}
