from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from ..database import get_db
from ..models.settings import SystemSettings
from ..schemas.settings import SystemSettingsUpdate, SystemSettingsResponse
from .auth import get_current_user
from ..models import User

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


def get_admin_user(current_user: User = Depends(get_current_user)) -> User:
    """관리자 권한 확인 (임시: 모든 로그인 사용자 허용)"""
    return current_user


def get_or_create_settings(db: Session) -> SystemSettings:
    """시스템 설정 조회 또는 기본값 생성"""
    settings = db.query(SystemSettings).first()
    if not settings:
        settings = SystemSettings(
            search_page_size=20,
            korea_margin_percent=5.0,
            mongolia_margin_percent=5.0,
            cc_per_usdc=1,  # 1 USD = 1 CC
            cc_per_view=1,  # 차량 상세 조회 시 1 CC
            cars_per_cc=3,  # 1 CC = 3 recommended vehicles per request
            cc_signup_bonus=3,  # 3 CC free on signup
            cache_ttl_hours=2,
            container_logistics_usd=3600,
            shoring_cost_usd=300,
        )
        db.add(settings)
        db.commit()
        db.refresh(settings)
    return settings


# ==================== Public Endpoints ====================

@router.get("/", response_model=SystemSettingsResponse)
def get_system_settings(db: Session = Depends(get_db)):
    """시스템 설정 조회 (Public)"""
    return get_or_create_settings(db)


@router.get("/search-page-size")
def get_search_page_size(db: Session = Depends(get_db)):
    """검색 결과 페이지 크기 조회 (Public)"""
    settings = get_or_create_settings(db)
    return {"search_page_size": settings.search_page_size}


# ==================== Admin Endpoints ====================

@router.put("/", response_model=SystemSettingsResponse)
def update_system_settings(
    settings_data: SystemSettingsUpdate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_admin_user)
):
    """시스템 설정 수정 (Admin)"""
    settings = get_or_create_settings(db)

    update_data = settings_data.model_dump(exclude_unset=True)
    for field, value in update_data.items():
        setattr(settings, field, value)

    db.commit()
    db.refresh(settings)
    return settings
