# app/api/v1/vendor/settings.py
"""
Vendor settings and configuration endpoints.

Vendor Context: Uses token_vendor_id from JWT token (authenticated vendor API pattern).
The get_current_vendor_api dependency guarantees token_vendor_id is present.
"""

import logging

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

from app.api.deps import get_current_vendor_api
from app.core.database import get_db
from app.services.vendor_service import vendor_service
from models.database.user import User

router = APIRouter(prefix="/settings")
logger = logging.getLogger(__name__)


@router.get("")
def get_vendor_settings(
    current_user: User = Depends(get_current_vendor_api),
    db: Session = Depends(get_db),
):
    """Get vendor settings and configuration."""
    vendor = vendor_service.get_vendor_by_id(db, current_user.token_vendor_id)

    return {
        "vendor_code": vendor.vendor_code,
        "subdomain": vendor.subdomain,
        "name": vendor.name,
        "contact_email": vendor.contact_email,
        "contact_phone": vendor.contact_phone,
        "website": vendor.website,
        "business_address": vendor.business_address,
        "tax_number": vendor.tax_number,
        "letzshop_csv_url_fr": vendor.letzshop_csv_url_fr,
        "letzshop_csv_url_en": vendor.letzshop_csv_url_en,
        "letzshop_csv_url_de": vendor.letzshop_csv_url_de,
        "is_active": vendor.is_active,
        "is_verified": vendor.is_verified,
    }


@router.put("/marketplace")
def update_marketplace_settings(
    marketplace_config: dict,
    current_user: User = Depends(get_current_vendor_api),
    db: Session = Depends(get_db),
):
    """Update marketplace integration settings."""
    # Service handles permission checking and raises InsufficientPermissionsException if needed
    return vendor_service.update_marketplace_settings(
        db, current_user.token_vendor_id, marketplace_config, current_user
    )
