from pydantic import BaseModel, Field, constr, confloat, validator
from typing import Optional, List
from datetime import datetime

# ===================================================
# BookRankingモデル
# ===================================================

# // 書籍ランキングデータの基本フィールドを定義するベースモデル
class BookRankingBase(BaseModel):
    title: str
    source: str

# // 新しいランキングをDBに作成する際に使用するモデル
class BookRankingCreate(BookRankingBase):
    pass

# // DBからランキング情報を読み込む際に使用するモデル
class BookRanking(BookRankingBase):
    id: int
    created_at: datetime

    class Config:
        # // ORMオブジェクトからPydanticモデルへの変換を許可します
        from_attributes = True


# ===================================================
# Keywordモデル
# ===================================================

# // キーワードデータの基本フィールドを定義するベースモデル
class KeywordBase(BaseModel):
    keyword: str

# // 新しいキーワードをDBに作成する際に使用するモデル
class KeywordCreate(KeywordBase):
    pass

# // DBからキーワード情報を読み込む際に使用するモデル
class Keyword(KeywordBase):
    id: int
    created_at: datetime

    class Config:
        # // ORMオブジェクトからPydanticモデルへの変換を許可します
        from_attributes = True


# ===================================================
# ChatHistoryモデル (新規追加)
# ===================================================

# // 会話履歴レコードの基本フィールドを定義するベースモデル
class ChatHistoryBase(BaseModel):
    # // どの会話セッションに属するかを識別するためのID
    session_id: str
    # // 会話の順序
    sort_order: int
    # // ユーザーのメッセージ（存在しない場合もあるためOptional）
    user_message: Optional[str] = None
    # // アシスタントの応答（存在しない場合もあるためOptional）
    assistant_response: Optional[str] = None

# // 新しい会話履歴をDBに作成する際に使用するモデル
class ChatHistoryCreate(ChatHistoryBase):
    pass

# // DBから会話履歴を読み込む際に使用するモデル
class ChatHistory(ChatHistoryBase):
    # // レコードのユニークID
    id: int
    # // レコードの作成日時
    created_at: datetime

    class Config:
        # // ORMオブジェクトからPydanticモデルへの変換を許可します
        from_attributes = True


# ===================================================
# API入出力用スキーマ
# ===================================================
class SpeechText(BaseModel):
    # // 音声に変換するテキスト
    text: str
    # // ユーザーを識別するためのトークン
    chat_token: str

# --- ▼ [追加] チャットリクエストを受け取るためのスキーマ ▼ ---
class ChatRequest(BaseModel):
    # // ユーザーが送信した最新のメッセージ
    message: str


class ChatMessage(BaseModel):
    message: str
    lineuser_id: int


class QuizMessage(BaseModel):
    message: str
    response: str
    lineuser_id: int


class TokenData(BaseModel):
    id_token: constr(strip_whitespace=True, min_length=1) = Field(..., example="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")


class LineUser(BaseModel):
    lineuser_id: int


class MedicineText(BaseModel):
    text: str
    chat_token: str


class UserQuestion(BaseModel):
    question: str
    chat_token: str
