from __future__ import annotations

from sqlalchemy import DECIMAL, BigInteger, Date, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint, text
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Department(Base):
    __tablename__ = "sys_department"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    dept_code: Mapped[str] = mapped_column(String(50))
    dept_name: Mapped[str] = mapped_column(String(100))
    parent_id: Mapped[int | None] = mapped_column(ForeignKey("sys_department.id"), nullable=True)
    org_node_type: Mapped[str] = mapped_column(String(32), server_default=text("'DEPARTMENT'"))
    dept_type: Mapped[str] = mapped_column(String(32))
    manager_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    manager_employee_id: Mapped[int | None] = mapped_column(ForeignKey("hr_employee.id"), nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    sort_no: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Employee(Base):
    __tablename__ = "hr_employee"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    employee_code: Mapped[str] = mapped_column(String(50))
    employee_name: Mapped[str] = mapped_column(String(100))
    dept_id: Mapped[int] = mapped_column(ForeignKey("sys_department.id"))
    mobile: Mapped[str | None] = mapped_column(String(30), nullable=True)
    gender: Mapped[str | None] = mapped_column(String(16), nullable=True)
    hire_date: Mapped[object | None] = mapped_column(Date, nullable=True)
    job_title: Mapped[str | None] = mapped_column(String(100), nullable=True)
    shift_code: Mapped[str | None] = mapped_column(String(32), nullable=True)
    manager_employee_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    is_operator: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    is_workshop_staff: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Role(Base):
    __tablename__ = "sys_role"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    role_code: Mapped[str] = mapped_column(String(50))
    role_name: Mapped[str] = mapped_column(String(100))
    role_scope: Mapped[str] = mapped_column(String(32))
    status: Mapped[str] = mapped_column(String(32))
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class Permission(Base):
    __tablename__ = "sys_permission"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    permission_code: Mapped[str] = mapped_column(String(100))
    permission_name: Mapped[str] = mapped_column(String(100))
    module_code: Mapped[str] = mapped_column(String(50))
    action_code: Mapped[str] = mapped_column(String(50))
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class RolePermission(Base):
    __tablename__ = "sys_role_permission"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    role_id: Mapped[int] = mapped_column(ForeignKey("sys_role.id"))
    permission_id: Mapped[int] = mapped_column(ForeignKey("sys_permission.id"))
    created_at: Mapped[object] = mapped_column(DateTime)


class User(Base):
    __tablename__ = "sys_user"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    username: Mapped[str] = mapped_column(String(50))
    password_hash: Mapped[str] = mapped_column(String(255))
    employee_id: Mapped[int] = mapped_column(ForeignKey("hr_employee.id"))
    dept_id: Mapped[int] = mapped_column(ForeignKey("sys_department.id"))
    nickname: Mapped[str | None] = mapped_column(String(100), nullable=True)
    email: Mapped[str | None] = mapped_column(String(100), nullable=True)
    is_super_admin: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    last_login_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    status: Mapped[str] = mapped_column(String(32))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class UserRole(Base):
    __tablename__ = "sys_user_role"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id"))
    role_id: Mapped[int] = mapped_column(ForeignKey("sys_role.id"))
    created_at: Mapped[object] = mapped_column(DateTime)


class OrgEmployeeBinding(Base):
    __tablename__ = "sys_org_employee_binding"
    __table_args__ = (UniqueConstraint("dept_id", "employee_id", name="uk_org_employee_binding"),)

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    dept_id: Mapped[int] = mapped_column(ForeignKey("sys_department.id"))
    employee_id: Mapped[int] = mapped_column(ForeignKey("hr_employee.id"))
    sort_no: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32), server_default=text("'ACTIVE'"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class OrgManagerBinding(Base):
    __tablename__ = "sys_org_manager_binding"
    __table_args__ = (UniqueConstraint("dept_id", "employee_id", name="uk_org_manager_binding"),)

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    dept_id: Mapped[int] = mapped_column(ForeignKey("sys_department.id"))
    employee_id: Mapped[int] = mapped_column(ForeignKey("hr_employee.id"))
    sort_no: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32), server_default=text("'ACTIVE'"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class BroadcastMessage(Base):
    __tablename__ = "sys_broadcast_message"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    title: Mapped[str] = mapped_column(String(100))
    content: Mapped[str] = mapped_column(String(500))
    tone: Mapped[str] = mapped_column(String(32), server_default=text("'INFO'"))
    status: Mapped[str] = mapped_column(String(32), server_default=text("'ACTIVE'"))
    starts_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    ends_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    sort_no: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    created_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class SystemConfig(Base):
    __tablename__ = "sys_system_config"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
    config_code: Mapped[str] = mapped_column(String(100), unique=True)
    config_name: Mapped[str] = mapped_column(String(100))
    config_value: Mapped[str] = mapped_column(String(500))
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    status: Mapped[str] = mapped_column(String(32), server_default=text("'ACTIVE'"))
    created_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    updated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class AiAssistantConfig(Base):
    __tablename__ = "sys_ai_assistant_config"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    assistant_name: Mapped[str] = mapped_column(String(100), server_default=text("'嘉恒工艺助手'"))
    provider_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    api_base_url: Mapped[str | None] = mapped_column(String(255), nullable=True)
    api_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
    model_name: Mapped[str | None] = mapped_column(String(100), nullable=True)
    temperature: Mapped[float] = mapped_column(DECIMAL(6, 3), server_default=text("0.300"))
    system_prompt: Mapped[str | None] = mapped_column(Text, nullable=True)
    enabled: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    created_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    updated_by: Mapped[int | None] = mapped_column(ForeignKey("sys_user.id"), nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)
