# -*- coding: utf-8 -*-

from fastapi import FastAPI, Form, HTTPException, Request,status,APIRouter,Query,Path
from tools.loglog import logger,log_err_e

from core.respone_format import *
from gpt.get_article2 import GetArticle
from pydantic import BaseModel, ValidationError, conint,Field
from typing import List, Optional,Literal


router = APIRouter()
get_article = GetArticle()

class Word(BaseModel):
    meaning_id:int = Field(..., description="单词的词义id")
    word_id:int = Field(..., description="单词id")
    spell: str = Field(..., description="单词的拼写")
    meaning: str = Field(..., description="单词的意思")


class ArticleRequest(BaseModel):
    core_words: List[Word] = Field(..., description="单词列表")
    extend_words: List[Word] = Field(..., description="单词列表")
    take_count: int = 2 
    student_stage: Literal[1, 2, 3] 
    demo_name: Optional[str] = "无" 
    reading_level: int = Field(default=-1, description="阅读水平，默认值为-1")


@router.post("/article/reading-comprehension")
def post_article(json_data:ArticleRequest,request:Request):
    json_data = json_data.dict()
    real_ip = request.headers.get("X-Real-IP","0.0.0.0")
    core_words,extend_words,take_count,student_stage,demo_name = json_data["core_words"],json_data["extend_words"],json_data["take_count"],json_data["student_stage"],json_data["demo_name"]
    reading_level = json_data.get("reading_level",-1) 

    try:
        r = get_article.submit_task(core_words=core_words,extend_words=extend_words, take_count=take_count,student_stage=student_stage,real_ip=real_ip,demo_name=demo_name)
        return r if not isinstance(r,str) else resp_500(message=r)

    except Exception as e:
        log_err_e(e,msg="文章2接口错误/article/reading-comprehension；")
        return resp_500(message=f"{type(e).__name__},{e}")