# -*- coding: utf-8 -*-
"""
STEP108-03 SVG Default Realtor Avatar

중개사 대표사진이 없을 때 사용하는 기본 사람 픽토그램 SVG 생성 유틸.
"""

from __future__ import annotations

from pathlib import Path
import re


DEFAULT_BG_COLOR = "#F1F3F5"
DEFAULT_ICON_COLOR = "#8A8F98"
DEFAULT_STROKE_COLOR = "#D7DCE2"


def _safe_hex_color(value: str, fallback: str) -> str:
    value = str(value or "").strip()

    if re.fullmatch(r"#[0-9A-Fa-f]{6}", value):
        return value.upper()

    if re.fullmatch(r"#[0-9A-Fa-f]{3}", value):
        return value.upper()

    return fallback


def make_default_realtor_avatar_svg(
    size: int = 96,
    bg_color: str = DEFAULT_BG_COLOR,
    icon_color: str = DEFAULT_ICON_COLOR,
    stroke_color: str = DEFAULT_STROKE_COLOR,
) -> str:
    """
    기본 회색 계열 사람 얼굴/상반신 SVG를 문자열로 반환한다.
    """

    try:
        size = int(size)
    except Exception:
        size = 96

    if size < 48:
        size = 48

    if size > 512:
        size = 512

    bg_color = _safe_hex_color(bg_color, DEFAULT_BG_COLOR)
    icon_color = _safe_hex_color(icon_color, DEFAULT_ICON_COLOR)
    stroke_color = _safe_hex_color(stroke_color, DEFAULT_STROKE_COLOR)

    return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{size}" height="{size}" viewBox="0 0 96 96" role="img" aria-label="default realtor profile">
  <circle cx="48" cy="48" r="47" fill="{bg_color}" stroke="{stroke_color}" stroke-width="2"/>
  <circle cx="48" cy="36" r="15" fill="none" stroke="{icon_color}" stroke-width="7" stroke-linecap="round"/>
  <path d="M22 82c3.6-18.2 15.1-28 26-28s22.4 9.8 26 28" fill="none" stroke="{icon_color}" stroke-width="8" stroke-linecap="round"/>
  <path d="M37 35c2.5-8.5 8.7-12.8 17-12.2 6.3.5 11.3 4.1 13.7 9.4" fill="none" stroke="{icon_color}" stroke-width="7" stroke-linecap="round"/>
</svg>
"""


def ensure_default_realtor_avatar_svg(
    output_path: str | Path,
    size: int = 96,
    bg_color: str = DEFAULT_BG_COLOR,
    icon_color: str = DEFAULT_ICON_COLOR,
    stroke_color: str = DEFAULT_STROKE_COLOR,
    force: bool = False,
) -> dict:
    """
    SVG 파일이 없으면 생성한다.
    force=True면 항상 덮어쓴다.
    """

    path = Path(output_path)

    if path.exists() and not force:
        return {
            "ok": True,
            "created": False,
            "path": str(path),
            "message": "exists",
        }

    path.parent.mkdir(parents=True, exist_ok=True)

    svg = make_default_realtor_avatar_svg(
        size=size,
        bg_color=bg_color,
        icon_color=icon_color,
        stroke_color=stroke_color,
    )

    path.write_text(svg, encoding="utf-8")

    return {
        "ok": True,
        "created": True,
        "path": str(path),
        "message": "created",
    }


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--output", required=True)
    parser.add_argument("--size", type=int, default=96)
    parser.add_argument("--bg-color", default=DEFAULT_BG_COLOR)
    parser.add_argument("--icon-color", default=DEFAULT_ICON_COLOR)
    parser.add_argument("--stroke-color", default=DEFAULT_STROKE_COLOR)
    parser.add_argument("--force", action="store_true")

    args = parser.parse_args()

    result = ensure_default_realtor_avatar_svg(
        output_path=args.output,
        size=args.size,
        bg_color=args.bg_color,
        icon_color=args.icon_color,
        stroke_color=args.stroke_color,
        force=args.force,
    )

    print(result)
