from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, StreamingResponse
from sqlalchemy.orm import Session
from sqlalchemy import func, and_, or_
from datetime import datetime, date
from typing import List
import io
import re
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter
from openpyxl.drawing.image import Image as XLImage
import os

from database.database import get_db
from database.models import Issue, IssueStatus, IssueCategory, User, UserRole, ReviewStatus
from database import schemas
from routers.auth import get_current_user
from routers.page_permissions import check_page_access
from utils.tkuser_client import get_token_from_request
import utils.tkuser_client as tkuser_client

router = APIRouter(prefix="/api/reports", tags=["reports"])

@router.post("/summary", response_model=schemas.ReportSummary)
async def generate_report_summary(
    report_request: schemas.ReportRequest,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """보고서 요약 생성"""
    start_date = report_request.start_date
    end_date = report_request.end_date

    total_hours = 0

    # 이슈 통계
    issues_query = db.query(Issue).filter(
        Issue.report_date >= start_date,
        Issue.report_date <= end_date
    )
    
    # 일반 사용자는 자신의 이슈만
    if current_user.role == UserRole.user:
        issues_query = issues_query.filter(Issue.reporter_id == current_user.id)
    
    issues = issues_query.all()
    
    # 카테고리별 통계
    category_stats = schemas.CategoryStats()
    completed_issues = 0
    total_resolution_time = 0
    resolved_count = 0
    
    for issue in issues:
        # 카테고리별 카운트
        if issue.category == IssueCategory.material_missing:
            category_stats.material_missing += 1
        elif issue.category == IssueCategory.design_error:
            category_stats.dimension_defect += 1
        elif issue.category == IssueCategory.incoming_defect:
            category_stats.incoming_defect += 1
        
        # 완료된 이슈
        if issue.status == IssueStatus.complete:
            completed_issues += 1
            if issue.work_hours > 0:
                total_resolution_time += issue.work_hours
                resolved_count += 1
    
    # 평균 해결 시간
    average_resolution_time = total_resolution_time / resolved_count if resolved_count > 0 else 0
    
    return schemas.ReportSummary(
        start_date=start_date,
        end_date=end_date,
        total_hours=total_hours,
        total_issues=len(issues),
        category_stats=category_stats,
        completed_issues=completed_issues,
        average_resolution_time=average_resolution_time
    )

@router.get("/issues")
async def get_report_issues(
    start_date: datetime,
    end_date: datetime,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """보고서용 이슈 상세 목록"""
    query = db.query(Issue).filter(
        Issue.report_date >= start_date,
        Issue.report_date <= end_date
    )
    
    # 일반 사용자는 자신의 이슈만
    if current_user.role == UserRole.user:
        query = query.filter(Issue.reporter_id == current_user.id)
    
    issues = query.order_by(Issue.report_date.desc()).all()
    
    return [{
        "id": issue.id,
        "photo_path": issue.photo_path,
        "category": issue.category,
        "description": issue.description,
        "status": issue.status,
        "reporter_name": issue.reporter.full_name or issue.reporter.username,
        "report_date": issue.report_date,
        "work_hours": issue.work_hours,
        "detail_notes": issue.detail_notes
    } for issue in issues]

@router.get("/daily-preview")
async def preview_daily_report(
    project_id: int,
    request: Request,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """일일보고서 미리보기 - 추출될 항목 목록"""

    # 권한 확인: admin 또는 reports/reports_daily 페이지 권한 보유자
    if current_user.role != UserRole.admin and not check_page_access(current_user.id, 'reports', db) and not check_page_access(current_user.id, 'reports_daily', db):
        raise HTTPException(status_code=403, detail="보고서 접근 권한이 없습니다")

    # 프로젝트 확인 (tkuser API)
    token = get_token_from_request(request)
    project = await tkuser_client.get_project(token, project_id)
    if not project:
        raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다")

    # 추출될 항목 조회 (진행 중 + 미추출 완료 항목)
    issues_query = db.query(Issue).filter(
        Issue.project_id == project_id,
        or_(
            Issue.review_status == ReviewStatus.in_progress,
            and_(
                Issue.review_status == ReviewStatus.completed,
                Issue.last_exported_at == None
            )
        )
    )

    issues = issues_query.all()

    # 정렬: 지연 -> 진행중 -> 완료됨 순으로, 같은 상태 내에서는 신고일 최신순
    issues = sorted(issues, key=lambda x: (get_issue_priority(x), -x.report_date.timestamp() if x.report_date else 0))

    # 통계 계산
    stats = calculate_project_stats(issues)

    # 이슈 리스트를 schema로 변환 후 명시적 직렬화 (브라우저 호환성)
    issues_data = [schemas.Issue.from_orm(issue) for issue in issues]

    return JSONResponse(content=jsonable_encoder({
        "project": project,
        "stats": stats,
        "issues": issues_data,
        "total_issues": len(issues)
    }))

@router.post("/daily-export")
async def export_daily_report(
    report_req: schemas.DailyReportRequest,
    request: Request,
    current_user: User = Depends(get_current_user),
    db: Session = Depends(get_db)
):
    """일일보고서 엑셀 내보내기"""

    # 권한 확인: admin 또는 reports/reports_daily 페이지 권한 보유자
    if current_user.role != UserRole.admin and not check_page_access(current_user.id, 'reports', db) and not check_page_access(current_user.id, 'reports_daily', db):
        raise HTTPException(status_code=403, detail="보고서 접근 권한이 없습니다")

    # 프로젝트 확인 (tkuser API)
    token = get_token_from_request(request)
    project = await tkuser_client.get_project(token, report_req.project_id)
    if not project:
        raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다")
    
    # 관리함 데이터 조회
    # 1. 진행 중인 항목 (모두 포함)
    in_progress_only = db.query(Issue).filter(
        Issue.project_id == report_req.project_id,
        Issue.review_status == ReviewStatus.in_progress
    ).all()

    # 2. 완료된 항목 (모두 조회)
    all_completed = db.query(Issue).filter(
        Issue.project_id == report_req.project_id,
        Issue.review_status == ReviewStatus.completed
    ).all()

    # 완료 항목 중 "완료 후 추출 안된 것" 필터링
    # 규칙: 완료 처리 후 1회에 한해서만 "진행 중" 시트에 표시
    not_exported_after_completion = []
    for issue in all_completed:
        if issue.last_exported_at is None:
            # 한번도 추출 안됨 -> 진행 중 시트에 표시 (완료 후 첫 추출)
            not_exported_after_completion.append(issue)
        elif issue.actual_completion_date:
            # actual_completion_date가 있는 경우: 완료일과 마지막 추출일 비교
            completion_date = issue.actual_completion_date.replace(tzinfo=None) if issue.actual_completion_date.tzinfo else issue.actual_completion_date
            export_date = issue.last_exported_at.replace(tzinfo=None) if issue.last_exported_at.tzinfo else issue.last_exported_at
            if completion_date > export_date:
                # 완료일이 마지막 추출일보다 나중 -> 완료 후 아직 추출 안됨 -> 진행 중 시트에 표시
                not_exported_after_completion.append(issue)
            # else: 완료일이 마지막 추출일보다 이전 -> 이미 완료 후 추출됨 -> 완료됨 시트로
        # else: actual_completion_date가 없고 last_exported_at가 있음
        #       -> 이미 한번 이상 추출됨 -> 완료됨 시트로

    # "진행 중" 시트용: 진행 중 + 완료되고 아직 추출 안된 것
    in_progress_issues = in_progress_only + not_exported_after_completion

    # 진행 중 시트 정렬: 지연중 -> 진행중 -> 완료됨 순서
    in_progress_issues = sorted(in_progress_issues, key=lambda x: (get_issue_priority(x), -x.report_date.timestamp() if x.report_date else 0))

    # "완료됨" 시트용: 완료 항목 중 "완료 후 추출된 것"만 (진행 중 시트에 표시되는 것 제외)
    not_exported_ids = {issue.id for issue in not_exported_after_completion}
    completed_issues = [issue for issue in all_completed if issue.id not in not_exported_ids]

    # 완료됨 시트도 정렬 (완료일 최신순)
    completed_issues = sorted(completed_issues, key=lambda x: -x.actual_completion_date.timestamp() if x.actual_completion_date else 0)

    # 웹과 동일한 로직: 진행중 + 완료를 함께 정렬하여 순번 할당
    # (웹에서는 in_progress와 completed를 함께 가져와서 전체를 reviewed_at 순으로 정렬 후 순번 매김)
    all_management_issues = in_progress_only + all_completed

    # reviewed_at 기준으로 정렬 (웹과 동일)
    all_management_issues = sorted(all_management_issues, key=lambda x: x.reviewed_at if x.reviewed_at else datetime.min)

    # 프로젝트별로 그룹화하여 순번 할당 (웹과 동일한 로직)
    project_groups = {}
    for issue in all_management_issues:
        if issue.project_id not in project_groups:
            project_groups[issue.project_id] = []
        project_groups[issue.project_id].append(issue)

    # 각 프로젝트별로 순번 재할당 (웹과 동일)
    for project_id, project_issues in project_groups.items():
        for idx, issue in enumerate(project_issues, 1):
            issue._display_no = idx

    # 전체 이슈 (통계 계산용 및 추출 이력 업데이트용)
    issues = list(set(in_progress_only + all_completed))  # 중복 제거
    
    # 통계 계산
    stats = calculate_project_stats(issues)
    
    # 엑셀 파일 생성
    wb = Workbook()

    # 스타일 정의 (공통)
    header_font = Font(bold=True, color="FFFFFF")
    header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
    stats_font = Font(bold=True, size=12)
    stats_fill = PatternFill(start_color="E7E6E6", end_color="E7E6E6", fill_type="solid")
    border = Border(
        left=Side(style='thin'),
        right=Side(style='thin'),
        top=Side(style='thin'),
        bottom=Side(style='thin')
    )
    center_alignment = Alignment(horizontal='center', vertical='center')
    card_header_font = Font(bold=True, color="FFFFFF", size=11)
    label_font = Font(bold=True, size=10)
    label_fill = PatternFill(start_color="E7E6E6", end_color="E7E6E6", fill_type="solid")
    content_font = Font(size=10)
    thick_border = Border(
        left=Side(style='medium'),
        right=Side(style='medium'),
        top=Side(style='medium'),
        bottom=Side(style='medium')
    )

    # 두 개의 시트를 생성하고 각각 데이터 입력
    sheets_data = [
        (wb.active, in_progress_issues, "진행 중"),
        (wb.create_sheet(title="완료됨"), completed_issues, "완료됨")
    ]

    sheets_data[0][0].title = "진행 중"

    for ws, sheet_issues, sheet_title in sheets_data:
        # 제목 및 기본 정보
        ws.merge_cells('A1:L1')
        ws['A1'] = f"{project['project_name']} - {sheet_title}"
        ws['A1'].font = Font(bold=True, size=16)
        ws['A1'].alignment = center_alignment

        ws.merge_cells('A2:L2')
        ws['A2'] = f"생성일: {datetime.now().strftime('%Y년 %m월 %d일')}"
        ws['A2'].alignment = center_alignment

        # 프로젝트 통계 (4행부터) - 진행 중 시트에만 표시
        if sheet_title == "진행 중":
            ws.merge_cells('A4:L4')
            ws['A4'] = "📊 프로젝트 현황"
            ws['A4'].font = Font(bold=True, size=14, color="FFFFFF")
            ws['A4'].fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
            ws['A4'].alignment = center_alignment
            ws.row_dimensions[4].height = 25

            # 통계 데이터 - 박스 형태로 개선
            stats_row = 5
            ws.row_dimensions[stats_row].height = 30

            # 총 신고 수량 (파란색 계열)
            ws.merge_cells(f'A{stats_row}:B{stats_row}')
            ws[f'A{stats_row}'] = "총 신고 수량"
            ws[f'A{stats_row}'].font = Font(bold=True, size=11, color="FFFFFF")
            ws[f'A{stats_row}'].fill = PatternFill(start_color="5B9BD5", end_color="5B9BD5", fill_type="solid")
            ws[f'A{stats_row}'].alignment = center_alignment

            ws[f'C{stats_row}'] = stats.total_count
            ws[f'C{stats_row}'].font = Font(bold=True, size=14)
            ws[f'C{stats_row}'].fill = PatternFill(start_color="DEEBF7", end_color="DEEBF7", fill_type="solid")
            ws[f'C{stats_row}'].alignment = center_alignment

            # 진행 현황 (노란색 계열)
            ws.merge_cells(f'D{stats_row}:E{stats_row}')
            ws[f'D{stats_row}'] = "진행 현황"
            ws[f'D{stats_row}'].font = Font(bold=True, size=11, color="000000")
            ws[f'D{stats_row}'].fill = PatternFill(start_color="FFD966", end_color="FFD966", fill_type="solid")
            ws[f'D{stats_row}'].alignment = center_alignment

            ws[f'F{stats_row}'] = stats.management_count
            ws[f'F{stats_row}'].font = Font(bold=True, size=14)
            ws[f'F{stats_row}'].fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
            ws[f'F{stats_row}'].alignment = center_alignment

            # 완료 현황 (초록색 계열)
            ws.merge_cells(f'G{stats_row}:H{stats_row}')
            ws[f'G{stats_row}'] = "완료 현황"
            ws[f'G{stats_row}'].font = Font(bold=True, size=11, color="FFFFFF")
            ws[f'G{stats_row}'].fill = PatternFill(start_color="70AD47", end_color="70AD47", fill_type="solid")
            ws[f'G{stats_row}'].alignment = center_alignment

            ws[f'I{stats_row}'] = stats.completed_count
            ws[f'I{stats_row}'].font = Font(bold=True, size=14)
            ws[f'I{stats_row}'].fill = PatternFill(start_color="C6E0B4", end_color="C6E0B4", fill_type="solid")
            ws[f'I{stats_row}'].alignment = center_alignment

            # 지연 중 (빨간색 계열)
            ws.merge_cells(f'J{stats_row}:K{stats_row}')
            ws[f'J{stats_row}'] = "지연 중"
            ws[f'J{stats_row}'].font = Font(bold=True, size=11, color="FFFFFF")
            ws[f'J{stats_row}'].fill = PatternFill(start_color="E74C3C", end_color="E74C3C", fill_type="solid")
            ws[f'J{stats_row}'].alignment = center_alignment

            ws[f'L{stats_row}'] = stats.delayed_count
            ws[f'L{stats_row}'].font = Font(bold=True, size=14)
            ws[f'L{stats_row}'].fill = PatternFill(start_color="FCE4D6", end_color="FCE4D6", fill_type="solid")
            ws[f'L{stats_row}'].alignment = center_alignment

            # 통계 박스에 테두리 적용
            thick_border = Border(
                left=Side(style='medium'),
                right=Side(style='medium'),
                top=Side(style='medium'),
                bottom=Side(style='medium')
            )

            for col in ['A', 'C', 'D', 'F', 'G', 'I', 'J', 'L']:
                if col in ['A', 'D', 'G', 'J']:  # 병합된 셀의 시작점
                    for c in range(ord(col), ord(col) + 2):  # 병합된 2개 셀
                        ws.cell(row=stats_row, column=c - ord('A') + 1).border = thick_border
                else:  # 숫자 셀
                    ws[f'{col}{stats_row}'].border = thick_border

        # 카드 형태로 데이터 입력 (완료됨 시트는 4행부터, 진행 중 시트는 7행부터)
        current_row = 4 if sheet_title == "완료됨" else 7

        for idx, issue in enumerate(sheet_issues, 1):
            card_start_row = current_row

            # 상태별 헤더 색상 설정
            header_color = get_issue_status_header_color(issue)
            card_header_fill = PatternFill(start_color=header_color, end_color=header_color, fill_type="solid")

            # 카드 헤더 (No, 상태, 신고일) - L열까지 확장
            ws.merge_cells(f'A{current_row}:C{current_row}')
            # 동적으로 할당된 프로젝트별 순번 사용 (웹과 동일)
            issue_no = getattr(issue, '_display_no', issue.project_sequence_no or issue.id)
            ws[f'A{current_row}'] = f"No. {issue_no}"
            ws[f'A{current_row}'].font = card_header_font
            ws[f'A{current_row}'].fill = card_header_fill
            ws[f'A{current_row}'].alignment = center_alignment

            ws.merge_cells(f'D{current_row}:G{current_row}')
            ws[f'D{current_row}'] = f"상태: {get_issue_status_text(issue)}"
            ws[f'D{current_row}'].font = card_header_font
            ws[f'D{current_row}'].fill = card_header_fill
            ws[f'D{current_row}'].alignment = center_alignment

            ws.merge_cells(f'H{current_row}:L{current_row}')
            ws[f'H{current_row}'] = f"신고일: {issue.report_date.strftime('%Y-%m-%d') if issue.report_date else '-'}"
            ws[f'H{current_row}'].font = card_header_font
            ws[f'H{current_row}'].fill = card_header_fill
            ws[f'H{current_row}'].alignment = center_alignment
            current_row += 1

            # 부적합명
            ws[f'A{current_row}'] = "부적합명"
            ws[f'A{current_row}'].font = label_font
            ws[f'A{current_row}'].fill = label_fill
            ws.merge_cells(f'B{current_row}:L{current_row}')

            # final_description이 있으면 사용, 없으면 description 사용
            issue_title = issue.final_description or issue.description or "내용 없음"
            ws[f'B{current_row}'] = issue_title
            ws[f'B{current_row}'].font = content_font
            ws[f'B{current_row}'].alignment = Alignment(wrap_text=True, vertical='top')
            current_row += 1

            # 상세내용 (detail_notes가 실제 상세 설명)
            if issue.detail_notes:
                ws[f'A{current_row}'] = "상세내용"
                ws[f'A{current_row}'].font = label_font
                ws[f'A{current_row}'].fill = label_fill
                ws.merge_cells(f'B{current_row}:L{current_row}')
                ws[f'B{current_row}'] = issue.detail_notes
                ws[f'B{current_row}'].font = content_font
                ws[f'B{current_row}'].alignment = Alignment(wrap_text=True, vertical='top')
                ws.row_dimensions[current_row].height = 50
                current_row += 1

            # 원인분류
            ws[f'A{current_row}'] = "원인분류"
            ws[f'A{current_row}'].font = label_font
            ws[f'A{current_row}'].fill = label_fill
            ws.merge_cells(f'B{current_row}:C{current_row}')
            ws[f'B{current_row}'] = get_category_text(issue.final_category or issue.category)
            ws[f'B{current_row}'].font = content_font

            ws[f'D{current_row}'] = "원인부서"
            ws[f'D{current_row}'].font = label_font
            ws[f'D{current_row}'].fill = label_fill
            ws.merge_cells(f'E{current_row}:F{current_row}')
            ws[f'E{current_row}'] = get_department_text(issue.cause_department)
            ws[f'E{current_row}'].font = content_font

            ws[f'G{current_row}'] = "신고자"
            ws[f'G{current_row}'].font = label_font
            ws[f'G{current_row}'].fill = label_fill
            ws.merge_cells(f'H{current_row}:L{current_row}')
            ws[f'H{current_row}'] = issue.reporter.full_name or issue.reporter.username if issue.reporter else "-"
            ws[f'H{current_row}'].font = content_font
            current_row += 1

            # 해결방안 (완료 반려 내용 및 댓글 제거)
            ws[f'A{current_row}'] = "해결방안"
            ws[f'A{current_row}'].font = label_font
            ws[f'A{current_row}'].fill = label_fill
            ws.merge_cells(f'B{current_row}:L{current_row}')

            # management_comment에서 완료 반려 패턴과 댓글 제거
            clean_solution = clean_management_comment_for_export(issue.management_comment)
            ws[f'B{current_row}'] = clean_solution
            ws[f'B{current_row}'].font = content_font
            ws[f'B{current_row}'].alignment = Alignment(wrap_text=True, vertical='center')  # 수직 가운데 정렬
            ws.row_dimensions[current_row].height = 30
            current_row += 1

            # 담당정보
            ws[f'A{current_row}'] = "담당부서"
            ws[f'A{current_row}'].font = label_font
            ws[f'A{current_row}'].fill = label_fill
            ws.merge_cells(f'B{current_row}:C{current_row}')
            ws[f'B{current_row}'] = get_department_text(issue.responsible_department)
            ws[f'B{current_row}'].font = content_font

            ws[f'D{current_row}'] = "담당자"
            ws[f'D{current_row}'].font = label_font
            ws[f'D{current_row}'].fill = label_fill
            ws.merge_cells(f'E{current_row}:G{current_row}')
            ws[f'E{current_row}'] = issue.responsible_person or ""
            ws[f'E{current_row}'].font = content_font

            ws[f'H{current_row}'] = "조치예상일"
            ws[f'H{current_row}'].font = label_font
            ws[f'H{current_row}'].fill = label_fill
            ws.merge_cells(f'I{current_row}:L{current_row}')
            ws[f'I{current_row}'] = issue.expected_completion_date.strftime('%Y-%m-%d') if issue.expected_completion_date else ""
            ws[f'I{current_row}'].font = content_font
            ws.row_dimensions[current_row].height = 20  # 기본 높이보다 20% 증가
            current_row += 1

            # === 신고 사진 영역 ===
            report_photos = [
                issue.photo_path,
                issue.photo_path2,
                issue.photo_path3,
                issue.photo_path4,
                issue.photo_path5
            ]
            report_photos = [p for p in report_photos if p]  # None 제거

            if report_photos:
                # 라벨 행 (A~L 전체 병합)
                ws.merge_cells(f'A{current_row}:L{current_row}')
                ws[f'A{current_row}'] = "신고 사진"
                ws[f'A{current_row}'].font = label_font
                ws[f'A{current_row}'].fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")  # 노란색
                ws[f'A{current_row}'].alignment = Alignment(horizontal='left', vertical='center')
                ws.row_dimensions[current_row].height = 18
                current_row += 1

                # 사진들을 한 행에 일렬로 표시 (간격 좁게)
                ws.merge_cells(f'A{current_row}:L{current_row}')
                report_image_inserted = False

                # 사진 위치: A, C, E, G, I (2열 간격)
                photo_columns = ['A', 'C', 'E', 'G', 'I']

                for idx, photo in enumerate(report_photos):
                    if idx >= 5:  # 최대 5장
                        break
                    photo_path = photo.replace('/uploads/', '/app/uploads/') if photo.startswith('/uploads/') else photo
                    if os.path.exists(photo_path):
                        try:
                            img = XLImage(photo_path)
                            img.width = min(img.width, 200)  # 크기 줄임
                            img.height = min(img.height, 150)
                            ws.add_image(img, f'{photo_columns[idx]}{current_row}')
                            report_image_inserted = True
                        except Exception as e:
                            print(f"이미지 삽입 실패 ({photo_path}): {e}")

                if report_image_inserted:
                    ws.row_dimensions[current_row].height = 120
                else:
                    ws[f'A{current_row}'] = "사진 파일을 찾을 수 없음"
                    ws[f'A{current_row}'].font = Font(size=9, italic=True, color="999999")
                    ws.row_dimensions[current_row].height = 20
                current_row += 1

            # === 완료 관련 정보 (완료된 항목만) ===
            if issue.review_status == ReviewStatus.completed:
                # 완료 코멘트
                if issue.completion_comment:
                    ws[f'A{current_row}'] = "완료 의견"
                    ws[f'A{current_row}'].font = label_font
                    ws[f'A{current_row}'].fill = PatternFill(start_color="D5F5D5", end_color="D5F5D5", fill_type="solid")
                    ws.merge_cells(f'B{current_row}:L{current_row}')
                    ws[f'B{current_row}'] = issue.completion_comment
                    ws[f'B{current_row}'].font = content_font
                    ws[f'B{current_row}'].alignment = Alignment(wrap_text=True, vertical='center')  # 수직 가운데 정렬
                    ws.row_dimensions[current_row].height = 30
                    current_row += 1

                # 완료 사진
                completion_photos = [
                    issue.completion_photo_path,
                    issue.completion_photo_path2,
                    issue.completion_photo_path3,
                    issue.completion_photo_path4,
                    issue.completion_photo_path5
                ]
                completion_photos = [p for p in completion_photos if p]  # None 제거

                if completion_photos:
                    # 라벨 행 (A~L 전체 병합)
                    ws.merge_cells(f'A{current_row}:L{current_row}')
                    ws[f'A{current_row}'] = "완료 사진"
                    ws[f'A{current_row}'].font = label_font
                    ws[f'A{current_row}'].fill = PatternFill(start_color="D5F5D5", end_color="D5F5D5", fill_type="solid")  # 연두색
                    ws[f'A{current_row}'].alignment = Alignment(horizontal='left', vertical='center')
                    ws.row_dimensions[current_row].height = 18
                    current_row += 1

                    # 사진들을 한 행에 일렬로 표시 (간격 좁게)
                    ws.merge_cells(f'A{current_row}:L{current_row}')
                    completion_image_inserted = False

                    # 사진 위치: A, C, E, G, I (2열 간격)
                    photo_columns = ['A', 'C', 'E', 'G', 'I']

                    for idx, photo in enumerate(completion_photos):
                        if idx >= 5:  # 최대 5장
                            break
                        photo_path = photo.replace('/uploads/', '/app/uploads/') if photo.startswith('/uploads/') else photo
                        if os.path.exists(photo_path):
                            try:
                                img = XLImage(photo_path)
                                img.width = min(img.width, 200)  # 크기 줄임
                                img.height = min(img.height, 150)
                                ws.add_image(img, f'{photo_columns[idx]}{current_row}')
                                completion_image_inserted = True
                            except Exception as e:
                                print(f"이미지 삽입 실패 ({photo_path}): {e}")

                    if completion_image_inserted:
                        ws.row_dimensions[current_row].height = 120
                    else:
                        ws[f'A{current_row}'] = "사진 파일을 찾을 수 없음"
                        ws[f'A{current_row}'].font = Font(size=9, italic=True, color="999999")
                        ws.row_dimensions[current_row].height = 20
                    current_row += 1

                # 완료일 정보
                if issue.actual_completion_date:
                    ws[f'A{current_row}'] = "완료일"
                    ws[f'A{current_row}'].font = label_font
                    ws[f'A{current_row}'].fill = PatternFill(start_color="D5F5D5", end_color="D5F5D5", fill_type="solid")
                    ws.merge_cells(f'B{current_row}:C{current_row}')
                    ws[f'B{current_row}'] = issue.actual_completion_date.strftime('%Y-%m-%d')
                    ws[f'B{current_row}'].font = content_font
                    current_row += 1

            # 사진이 하나도 없을 경우
            # 진행중: 신고 사진만 체크
            # 완료됨: 신고 사진 + 완료 사진 체크
            has_completion_photos = False
            if issue.review_status == ReviewStatus.completed:
                comp_photos = [p for p in [
                    issue.completion_photo_path,
                    issue.completion_photo_path2,
                    issue.completion_photo_path3,
                    issue.completion_photo_path4,
                    issue.completion_photo_path5
                ] if p]
                has_completion_photos = bool(comp_photos)
            has_any_photo = bool(report_photos) or has_completion_photos

            if not has_any_photo:
                ws[f'A{current_row}'] = "첨부사진"
                ws[f'A{current_row}'].font = label_font
                ws[f'A{current_row}'].fill = label_fill
                ws.merge_cells(f'B{current_row}:L{current_row}')
                ws[f'B{current_row}'] = "첨부된 사진 없음"
                ws[f'B{current_row}'].font = Font(size=9, italic=True, color="999999")
                current_row += 1

            # 카드 전체에 테두리 적용 (A-L 열)
            card_end_row = current_row - 1
            for row in range(card_start_row, card_end_row + 1):
                for col in range(1, 13):  # A-L 열 (12열)
                    cell = ws.cell(row=row, column=col)
                    if not cell.border or cell.border.left.style != 'medium':
                        cell.border = border

            # 카드 외곽에 굵은 테두리 (A-L 열) - 상태별 색상 적용
            border_color = header_color  # 헤더와 같은 색상 사용
            for col in range(1, 13):
                ws.cell(row=card_start_row, column=col).border = Border(
                    left=Side(style='medium' if col == 1 else 'thin', color=border_color),
                    right=Side(style='medium' if col == 12 else 'thin', color=border_color),
                    top=Side(style='medium', color=border_color),
                    bottom=Side(style='thin', color=border_color)
                )
                ws.cell(row=card_end_row, column=col).border = Border(
                    left=Side(style='medium' if col == 1 else 'thin', color=border_color),
                    right=Side(style='medium' if col == 12 else 'thin', color=border_color),
                    top=Side(style='thin', color=border_color),
                    bottom=Side(style='medium', color=border_color)
                )

            # 카드 좌우 테두리도 색상 적용
            for row in range(card_start_row + 1, card_end_row):
                ws.cell(row=row, column=1).border = Border(
                    left=Side(style='medium', color=border_color),
                    right=ws.cell(row=row, column=1).border.right if ws.cell(row=row, column=1).border else Side(style='thin'),
                    top=ws.cell(row=row, column=1).border.top if ws.cell(row=row, column=1).border else Side(style='thin'),
                    bottom=ws.cell(row=row, column=1).border.bottom if ws.cell(row=row, column=1).border else Side(style='thin')
                )
                ws.cell(row=row, column=12).border = Border(
                    left=ws.cell(row=row, column=12).border.left if ws.cell(row=row, column=12).border else Side(style='thin'),
                    right=Side(style='medium', color=border_color),
                    top=ws.cell(row=row, column=12).border.top if ws.cell(row=row, column=12).border else Side(style='thin'),
                    bottom=ws.cell(row=row, column=12).border.bottom if ws.cell(row=row, column=12).border else Side(style='thin')
                )

            # 카드 구분 (빈 행)
            current_row += 1
    
        # 열 너비 조정
        ws.column_dimensions['A'].width = 12  # 레이블 열
        ws.column_dimensions['B'].width = 15  # 내용 열
        ws.column_dimensions['C'].width = 15  # 내용 열
        ws.column_dimensions['D'].width = 15  # 내용 열
        ws.column_dimensions['E'].width = 15  # 내용 열
        ws.column_dimensions['F'].width = 15  # 내용 열
        ws.column_dimensions['G'].width = 15  # 내용 열
        ws.column_dimensions['H'].width = 15  # 내용 열
        ws.column_dimensions['I'].width = 15  # 내용 열
        ws.column_dimensions['J'].width = 15  # 내용 열
        ws.column_dimensions['K'].width = 15  # 내용 열
        ws.column_dimensions['L'].width = 15  # 내용 열
    
    # 엑셀 파일을 메모리에 저장
    excel_buffer = io.BytesIO()
    wb.save(excel_buffer)
    excel_buffer.seek(0)

    # 추출 이력 업데이트
    export_time = datetime.now()
    for issue in issues:
        issue.last_exported_at = export_time
        issue.export_count = (issue.export_count or 0) + 1
    db.commit()

    # 파일명 생성
    today = date.today().strftime('%Y%m%d')
    filename = f"{project['project_name']}_일일보고서_{today}.xlsx"

    # 한글 파일명을 위한 URL 인코딩 (ASCII 폴백 + UTF-8 인코딩 둘 다 제공)
    from urllib.parse import quote
    encoded_filename = quote(filename.encode('utf-8'))
    ascii_filename = f"daily_report_{today}.xlsx"

    excel_data = excel_buffer.read()
    return StreamingResponse(
        io.BytesIO(excel_data),
        media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        headers={
            "Content-Disposition": f"attachment; filename=\"{ascii_filename}\"; filename*=UTF-8''{encoded_filename}",
            "Content-Length": str(len(excel_data)),
        }
    )

def calculate_project_stats(issues: List[Issue]) -> schemas.DailyReportStats:
    """프로젝트 통계 계산"""
    stats = schemas.DailyReportStats()

    today = date.today()

    for issue in issues:
        stats.total_count += 1

        if issue.review_status == ReviewStatus.in_progress:
            stats.management_count += 1

            # 지연 여부 확인 (expected_completion_date가 오늘보다 이전인 경우)
            if issue.expected_completion_date:
                expected_date = issue.expected_completion_date.date() if isinstance(issue.expected_completion_date, datetime) else issue.expected_completion_date
                if expected_date < today:
                    stats.delayed_count += 1

        elif issue.review_status == ReviewStatus.completed:
            stats.completed_count += 1

    return stats

def get_category_text(category: IssueCategory) -> str:
    """카테고리 한글 변환"""
    category_map = {
        IssueCategory.material_missing: "자재 누락",
        IssueCategory.design_error: "설계 미스",
        IssueCategory.incoming_defect: "입고 불량",
        IssueCategory.inspection_miss: "검사 미스",
        IssueCategory.etc: "기타"
    }
    return category_map.get(category, str(category))

def get_department_text(department) -> str:
    """부서 한글 변환"""
    if not department:
        return ""
    
    department_map = {
        "production": "생산",
        "quality": "품질",
        "purchasing": "구매",
        "design": "설계",
        "sales": "영업"
    }
    return department_map.get(department, str(department))

def get_status_text(status: ReviewStatus) -> str:
    """상태 한글 변환"""
    status_map = {
        ReviewStatus.pending_review: "검토 대기",
        ReviewStatus.in_progress: "진행 중",
        ReviewStatus.completed: "완료됨",
        ReviewStatus.disposed: "폐기됨"
    }
    return status_map.get(status, str(status))

def clean_management_comment_for_export(text: str) -> str:
    """엑셀 내보내기용 management_comment 정리 (완료 반려, 댓글 제거)"""
    if not text:
        return ""

    # 1. 완료 반려 패턴 제거 ([완료 반려 - 날짜시간] 내용)
    text = re.sub(r'\[완료 반려[^\]]*\][^\n]*\n*', '', text)

    # 2. 댓글 패턴 제거 (└, ↳로 시작하는 줄들)
    lines = text.split('\n')
    filtered_lines = []
    for line in lines:
        # └ 또는 ↳로 시작하는 줄 제외
        if not re.match(r'^\s*[└↳]', line):
            filtered_lines.append(line)

    # 3. 빈 줄 정리
    result = '\n'.join(filtered_lines).strip()
    # 연속된 빈 줄을 하나로
    result = re.sub(r'\n{3,}', '\n\n', result)

    return result

def get_status_color(status: ReviewStatus) -> str:
    """상태별 색상 반환"""
    color_map = {
        ReviewStatus.in_progress: "FFF2CC",      # 연한 노랑
        ReviewStatus.completed: "E2EFDA",        # 연한 초록
        ReviewStatus.disposed: "F2F2F2"          # 연한 회색
    }
    return color_map.get(status, None)


def get_issue_priority(issue: Issue) -> int:
    """이슈 우선순위 반환 (엑셀 정렬용)
    1: 지연 (빨강)
    2: 진행중 (노랑)
    3: 완료됨 (초록)
    """
    if issue.review_status == ReviewStatus.completed:
        return 3
    elif issue.review_status == ReviewStatus.in_progress:
        # 조치 예상일이 지난 경우 지연
        if issue.expected_completion_date:
            today = date.today()
            expected_date = issue.expected_completion_date.date() if isinstance(issue.expected_completion_date, datetime) else issue.expected_completion_date
            if expected_date < today:
                return 1  # 지연
        return 2  # 진행중
    return 2

def get_issue_status_text(issue: Issue) -> str:
    """이슈 상태 텍스트 반환"""
    if issue.review_status == ReviewStatus.completed:
        return "완료됨"
    elif issue.review_status == ReviewStatus.in_progress:
        if issue.expected_completion_date:
            today = date.today()
            expected_date = issue.expected_completion_date.date() if isinstance(issue.expected_completion_date, datetime) else issue.expected_completion_date
            if expected_date < today:
                return "지연중"
        return "진행중"
    return get_status_text(issue.review_status)

def get_issue_status_header_color(issue: Issue) -> str:
    """이슈 상태별 헤더 색상 반환"""
    priority = get_issue_priority(issue)
    if priority == 1:  # 지연
        return "E74C3C"  # 빨간색
    elif priority == 2:  # 진행중
        return "FFD966"  # 노랑색 (주황색 사이)
    elif priority == 3:  # 완료
        return "92D050"  # 진한 초록색
    return "4472C4"  # 기본 파란색
