from __future__ import annotations

from typing import Optional

from fastapi import APIRouter
from pydantic import BaseModel, Field

from app.services.login_service import login_service
from app.services.send_service import send_service

router = APIRouter(prefix="/v1/wechat-personal", tags=["wechat-personal"])


class StartLoginRequest(BaseModel):
    account_id: str = Field(..., description="渠道账号唯一标识")
    account_name: Optional[str] = Field(None, description="账号显示名称")
    tenant_code: str = Field("default", description="业务系统租户编码")


class LoginConfirmRequest(BaseModel):
    tenant_code: str = Field("default", description="业务系统租户编码")


class SendTextRequest(BaseModel):
    account_id: str
    conversation_id: str = Field(..., description="目标会话/群 ID")
    text: str = Field(..., min_length=1, description="消息内容")
    record_id: str = Field(..., description="mci-server 发送记录 ID")
    tenant_code: str = Field("default", description="业务系统租户编码")


class SendImageRequest(BaseModel):
    account_id: str
    conversation_id: str
    image_url: str
    record_id: str = Field(..., description="mci-server 发送记录 ID")
    tenant_code: str = Field("default", description="业务系统租户编码")


@router.post("/login/start")
def start_login(request: StartLoginRequest):
    return login_service.start_login(request.account_id, request.account_name, request.tenant_code)


@router.get("/login/{account_id}/status")
def login_status(account_id: str):
    return login_service.get_status(account_id)


@router.post("/login/{account_id}/confirm")
def confirm_login(account_id: str, request: LoginConfirmRequest):
    return login_service.confirm_login(account_id, request.tenant_code)


@router.post("/login/{account_id}/expire")
def expire_login(account_id: str, request: LoginConfirmRequest):
    return login_service.expire_login(account_id, request.tenant_code)


@router.post("/send/text")
def send_text(request: SendTextRequest):
    return send_service.send_text(
        request.account_id,
        request.conversation_id,
        request.text,
        request.record_id,
        request.tenant_code,
    )


@router.post("/send/image")
def send_image(request: SendImageRequest):
    return send_service.send_image(
        request.account_id,
        request.conversation_id,
        request.image_url,
        request.record_id,
        request.tenant_code,
    )
