"""PDF 스타일 정의"""
from reportlab.lib import colors
from reportlab.lib.units import mm
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

# 색상 팔레트 (Money Forward 스타일)
COLORS = {
    "primary": colors.HexColor("#2563eb"),      # 파란색
    "secondary": colors.HexColor("#64748b"),    # 회색
    "success": colors.HexColor("#16a34a"),      # 녹색
    "danger": colors.HexColor("#dc2626"),       # 빨간색
    "warning": colors.HexColor("#f59e0b"),      # 노란색
    "info": colors.HexColor("#0891b2"),         # 청록색
    "purple": colors.HexColor("#8b5cf6"),       # 보라색
    "light_gray": colors.HexColor("#f1f5f9"),   # 연한 회색
    "dark_gray": colors.HexColor("#334155"),    # 진한 회색
    "border": colors.HexColor("#e2e8f0"),       # 테두리
    "white": colors.white,
    "black": colors.black,
}

# 차트 색상 팔레트
CHART_COLORS = [
    "#2563eb",  # 파란색
    "#16a34a",  # 녹색
    "#dc2626",  # 빨간색
    "#f59e0b",  # 노란색
    "#8b5cf6",  # 보라색
    "#0891b2",  # 청록색
    "#ec4899",  # 분홍색
    "#84cc16",  # 라임색
]

# 폰트 크기
FONT_SIZES = {
    "title": 24,
    "subtitle": 18,
    "heading1": 16,
    "heading2": 14,
    "heading3": 12,
    "body": 10,
    "small": 9,
    "tiny": 8,
}

# 여백
MARGINS = {
    "page_top": 40,
    "page_bottom": 30,
    "page_left": 40,
    "page_right": 40,
    "section": 20,
    "element": 10,
}

# 테이블 스타일
TABLE_STYLES = {
    "header_bg": COLORS["primary"],
    "header_text": COLORS["white"],
    "row_bg": COLORS["white"],
    "alt_row_bg": COLORS["light_gray"],
    "total_bg": colors.HexColor("#dbeafe"),
    "border": COLORS["border"],
}

# KPI 카드 스타일
KPI_CARD_STYLE = {
    "width": 180,
    "height": 80,
    "padding": 10,
    "border_radius": 8,
    "title_size": FONT_SIZES["small"],
    "value_size": FONT_SIZES["heading1"],
}

# 페이지 설정
PAGE_CONFIG = {
    "size": "A4",
    "orientation": "landscape",
    "width": 297 * mm,
    "height": 210 * mm,
}

# 일본어 폰트 경로 (시스템에 따라 조정 필요)
FONT_PATHS = {
    "gothic": [
        "/usr/share/fonts/truetype/fonts-japanese-gothic.ttf",
        "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    ],
    "gothic_bold": [
        "/usr/share/fonts/truetype/fonts-japanese-gothic.ttf",
        "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
    ],
}


def get_font_path(font_type: str = "gothic") -> str:
    """사용 가능한 폰트 경로 반환"""
    from pathlib import Path

    paths = FONT_PATHS.get(font_type, FONT_PATHS["gothic"])
    for path in paths:
        if Path(path).exists():
            return path

    # 기본 폰트 반환
    return "Helvetica"


def format_number(value, unit: str = "円") -> str:
    """숫자 포맷팅"""
    try:
        num = float(value)
        if unit == "%":
            return f"{num:.1f}%"
        elif abs(num) >= 100000000:  # 1억 이상
            return f"{num/100000000:.1f}億{unit}"
        elif abs(num) >= 10000:  # 1만 이상
            return f"{num/10000:.0f}万{unit}"
        else:
            return f"{num:,.0f}{unit}"
    except (ValueError, TypeError):
        return str(value)


def format_currency(value) -> str:
    """통화 포맷팅"""
    try:
        num = float(value)
        if num < 0:
            return f"▲{abs(num):,.0f}"
        return f"{num:,.0f}"
    except (ValueError, TypeError):
        return str(value)
