from datetime import datetime

from sqlalchemy import DateTime, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class Learner(Base):
    __tablename__ = "learners"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    phone: Mapped[str] = mapped_column(String(32), unique=True, index=True)
    current_name: Mapped[str] = mapped_column(String(64), index=True)
    id_type: Mapped[str | None] = mapped_column(String(32), nullable=True)
    id_no_encrypted: Mapped[str | None] = mapped_column(String(256), nullable=True)
    id_no_masked: Mapped[str | None] = mapped_column(String(64), nullable=True)
    student_no: Mapped[str | None] = mapped_column(String(64), nullable=True)
    status: Mapped[str] = mapped_column(String(32), default="active")
    remark: Mapped[str | None] = mapped_column(Text, nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class LearnerNameHistory(Base):
    __tablename__ = "learner_name_histories"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    learner_id: Mapped[int] = mapped_column(index=True)
    name: Mapped[str] = mapped_column(String(64))
    source: Mapped[str] = mapped_column(String(64), default="import")
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)


class LearnerPhoneHistory(Base):
    __tablename__ = "learner_phone_histories"

    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    learner_id: Mapped[int] = mapped_column(index=True)
    old_phone: Mapped[str] = mapped_column(String(32))
    new_phone: Mapped[str] = mapped_column(String(32))
    reason: Mapped[str] = mapped_column(String(255))
    changed_by: Mapped[int | None] = mapped_column(nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
