from __future__ import annotations

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

from app.models.base import Base


class MiniAppAttendancePoint(Base):
    __tablename__ = "attendance_points"

    name: Mapped[str] = mapped_column(String(128), primary_key=True)
    latitude: Mapped[float | None] = mapped_column(DECIMAL(10, 7), nullable=True)
    longitude: Mapped[float | None] = mapped_column(DECIMAL(10, 7), nullable=True)
    radius_meters: Mapped[int] = mapped_column(Integer, server_default=text("500"))
    remark: Mapped[str | None] = mapped_column(String(500), nullable=True)
    is_active: Mapped[bool] = mapped_column(Boolean, server_default=text("1"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MiniAppPersonnel(Base):
    __tablename__ = "personnel"

    phone: Mapped[str] = mapped_column(String(20), primary_key=True)
    name: Mapped[str] = mapped_column(String(64))
    is_temporary: Mapped[bool] = mapped_column(Boolean, server_default=text("0"))
    temporary_expires_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MiniAppPersonRole(Base):
    __tablename__ = "person_roles"

    phone: Mapped[str] = mapped_column(ForeignKey("personnel.phone"), primary_key=True)
    role: Mapped[str] = mapped_column(String(32), primary_key=True)
    created_at: Mapped[object] = mapped_column(DateTime)


class MiniAppPersonAttendancePoint(Base):
    __tablename__ = "person_attendance_points"

    phone: Mapped[str] = mapped_column(ForeignKey("personnel.phone"), primary_key=True)
    attendance_point_name: Mapped[str] = mapped_column(ForeignKey("attendance_points.name"), primary_key=True)
    created_at: Mapped[object] = mapped_column(DateTime)


class MiniAppProduct(Base):
    __tablename__ = "products"

    attendance_point_name: Mapped[str] = mapped_column(String(128), primary_key=True, server_default=text("'宁波嘉恒智能科技有限公司'"))
    project_no: Mapped[str] = mapped_column(String(64), primary_key=True)
    product_name: Mapped[str] = mapped_column(String(255), primary_key=True)
    device_no: Mapped[str] = mapped_column(String(64), primary_key=True)
    process_name: Mapped[str] = mapped_column(String(128), primary_key=True)
    profile_no: Mapped[str | None] = mapped_column(String(128), nullable=True)
    material_code: Mapped[str | None] = mapped_column(String(128), nullable=True)
    material_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
    supplier: Mapped[str | None] = mapped_column(String(255), nullable=True)
    product_net_weight_kg: Mapped[float | None] = mapped_column(DECIMAL(12, 4), nullable=True)
    product_gross_weight_kg: Mapped[float | None] = mapped_column(DECIMAL(12, 4), nullable=True)
    scrap_loss_rate: Mapped[float | None] = mapped_column(DECIMAL(10, 6), nullable=True)
    waste_price_yuan_per_kg: Mapped[float | None] = mapped_column(DECIMAL(12, 4), nullable=True)
    stamping_method: Mapped[str | None] = mapped_column(String(128), nullable=True)
    operator_count: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("1"))
    standard_beat: Mapped[float] = mapped_column(DECIMAL(12, 3), server_default=text("0"))
    standard_workload: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MiniAppProductionReport(Base):
    __tablename__ = "production_reports"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    attendance_point_name: Mapped[str] = mapped_column(String(128), server_default=text("'宁波嘉恒智能科技有限公司'"))
    session_id: Mapped[int] = mapped_column(BigInteger)
    employee_phone: Mapped[str] = mapped_column(ForeignKey("personnel.phone"))
    report_date: Mapped[object] = mapped_column(Date)
    start_at: Mapped[object] = mapped_column(DateTime)
    end_at: Mapped[object] = mapped_column(DateTime)
    duration_minutes: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    break_minutes: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    effective_minutes: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    total_good_qty: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    total_output_qty: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    actual_beat: Mapped[float] = mapped_column(DECIMAL(12, 3), server_default=text("0"))
    standard_beat: Mapped[float] = mapped_column(DECIMAL(12, 3), server_default=text("0"))
    expected_workload: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    pace_rate: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    workload_rate: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    status: Mapped[str] = mapped_column(String(32), server_default=text("'pending'"))
    reviewer_phone: Mapped[str | None] = mapped_column(String(20), nullable=True)
    reviewed_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    reject_reason: Mapped[str | None] = mapped_column(String(500), nullable=True)
    submitted_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MiniAppProductionReportItem(Base):
    __tablename__ = "production_report_items"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    report_id: Mapped[int] = mapped_column(ForeignKey("production_reports.id"))
    attendance_point_name: Mapped[str] = mapped_column(String(128), server_default=text("'宁波嘉恒智能科技有限公司'"))
    device_no: Mapped[str] = mapped_column(String(64))
    project_no: Mapped[str] = mapped_column(String(64))
    product_name: Mapped[str] = mapped_column(String(255))
    material_code: Mapped[str | None] = mapped_column(String(128), nullable=True)
    material_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
    raw_material_batch_no: Mapped[str | None] = mapped_column(String(128), nullable=True)
    process_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
    stamping_method: Mapped[str | None] = mapped_column(String(128), nullable=True)
    standard_beat: Mapped[float] = mapped_column(DECIMAL(12, 3), server_default=text("0"))
    standard_workload: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    good_qty: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    defect_qty: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    scrap_qty: Mapped[float] = mapped_column(DECIMAL(12, 2), server_default=text("0"))
    allocated_minutes: Mapped[float] = mapped_column(DECIMAL(10, 2), server_default=text("0"))
    started_at: Mapped[object | None] = mapped_column(DateTime, nullable=True)
    created_at: Mapped[object] = mapped_column(DateTime)


class MiniAppNotice(Base):
    __tablename__ = "notices"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    title: Mapped[str] = mapped_column(String(120))
    content: Mapped[str] = mapped_column(Text)
    sort_order: Mapped[int] = mapped_column(Integer, server_default=text("0"))
    is_active: Mapped[bool] = mapped_column(Boolean, server_default=text("1"))
    created_by: Mapped[str] = mapped_column(ForeignKey("personnel.phone"))
    created_at: Mapped[object] = mapped_column(DateTime)
    updated_at: Mapped[object] = mapped_column(DateTime)


class MiniAppNoticePoint(Base):
    __tablename__ = "notice_points"

    notice_id: Mapped[int] = mapped_column(ForeignKey("notices.id"), primary_key=True)
    attendance_point_name: Mapped[str] = mapped_column(ForeignKey("attendance_points.name"), primary_key=True)
    created_at: Mapped[object] = mapped_column(DateTime)
