-- 009_add_overtime_warning_columns.sql
-- monthly_summary 테이블에 12시간 초과(확인필요) 상태 컬럼 추가

-- monthly_summary 테이블에 컬럼 추가
ALTER TABLE monthly_summary 
ADD COLUMN overtime_warning_workers INT DEFAULT 0 COMMENT '확인필요(12시간초과) 작업자 수' AFTER error_workers,
ADD COLUMN has_overtime_warning BOOLEAN DEFAULT FALSE COMMENT '확인필요 상태 있음' AFTER has_errors;

-- UpdateDailySummary 프로시저 업데이트
DROP PROCEDURE IF EXISTS UpdateDailySummary;
DELIMITER //
CREATE PROCEDURE UpdateDailySummary(
    IN p_date DATE
)
BEGIN
    DECLARE v_year INT;
    DECLARE v_month INT;
    
    SET v_year = YEAR(p_date);
    SET v_month = MONTH(p_date);
    
    INSERT INTO monthly_summary (
        year, month, date,
        total_workers, working_workers,
        incomplete_workers, partial_workers, complete_workers,
        overtime_workers, vacation_workers, error_workers, overtime_warning_workers,
        total_work_hours, total_work_count, total_error_count,
        has_issues, has_errors, has_overtime_warning
    )
    SELECT 
        v_year, v_month, p_date,
        COUNT(*) as total_workers,
        COUNT(CASE WHEN work_status != 'incomplete' THEN 1 END) as working_workers,
        COUNT(CASE WHEN work_status = 'incomplete' THEN 1 END) as incomplete_workers,
        COUNT(CASE WHEN work_status = 'partial' THEN 1 END) as partial_workers,
        COUNT(CASE WHEN work_status IN ('complete', 'overtime', 'vacation-full', 'vacation-half', 'vacation-quarter', 'vacation-half-half') THEN 1 END) as complete_workers,
        COUNT(CASE WHEN work_status = 'overtime' THEN 1 END) as overtime_workers,
        COUNT(CASE WHEN work_status LIKE 'vacation%' THEN 1 END) as vacation_workers,
        COUNT(CASE WHEN work_status = 'error' THEN 1 END) as error_workers,
        COUNT(CASE WHEN work_status = 'overtime-warning' THEN 1 END) as overtime_warning_workers,
        SUM(total_work_hours) as total_work_hours,
        SUM(total_work_count) as total_work_count,
        SUM(error_work_count) as total_error_count,
        MAX(has_issues) as has_issues,
        MAX(has_error) as has_errors,
        MAX(CASE WHEN work_status = 'overtime-warning' THEN 1 ELSE 0 END) as has_overtime_warning
    FROM monthly_worker_status 
    WHERE date = p_date
    ON DUPLICATE KEY UPDATE
        total_workers = VALUES(total_workers),
        working_workers = VALUES(working_workers),
        incomplete_workers = VALUES(incomplete_workers),
        partial_workers = VALUES(partial_workers),
        complete_workers = VALUES(complete_workers),
        overtime_workers = VALUES(overtime_workers),
        vacation_workers = VALUES(vacation_workers),
        error_workers = VALUES(error_workers),
        overtime_warning_workers = VALUES(overtime_warning_workers),
        total_work_hours = VALUES(total_work_hours),
        total_work_count = VALUES(total_work_count),
        total_error_count = VALUES(total_error_count),
        has_issues = VALUES(has_issues),
        has_errors = VALUES(has_errors),
        has_overtime_warning = VALUES(has_overtime_warning),
        last_updated = CURRENT_TIMESTAMP;
END //
DELIMITER ;
