from datetime import date, datetime, time
from decimal import Decimal

from sqlalchemy import (
    Boolean,
    Date,
    DateTime,
    ForeignKey,
    Integer,
    Numeric,
    String,
    Text,
    Time,
    UniqueConstraint,
    func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.database import Base


class TimestampMixin:
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, server_default=func.now(), onupdate=func.now()
    )


class User(Base, TimestampMixin):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(120))
    email: Mapped[str] = mapped_column(String(160), unique=True, index=True)
    phone: Mapped[str | None] = mapped_column(String(30))
    password_hash: Mapped[str] = mapped_column(String(255))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

    roles: Mapped[list["Role"]] = relationship(secondary="user_roles", back_populates="users")


class Role(Base):
    __tablename__ = "roles"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(80), unique=True)
    description: Mapped[str | None] = mapped_column(String(255))
    is_system: Mapped[bool] = mapped_column(Boolean, default=False)

    users: Mapped[list[User]] = relationship(secondary="user_roles", back_populates="roles")
    permissions: Mapped[list["Permission"]] = relationship(
        secondary="role_permissions", back_populates="roles"
    )


class Permission(Base):
    __tablename__ = "permissions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    key: Mapped[str] = mapped_column(String(80), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(120))
    module: Mapped[str] = mapped_column(String(80))

    roles: Mapped[list[Role]] = relationship(secondary="role_permissions", back_populates="permissions")


class UserRole(Base):
    __tablename__ = "user_roles"
    __table_args__ = (UniqueConstraint("user_id", "role_id"),)

    user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True)
    role_id: Mapped[int] = mapped_column(ForeignKey("roles.id"), primary_key=True)


class RolePermission(Base):
    __tablename__ = "role_permissions"
    __table_args__ = (UniqueConstraint("role_id", "permission_id"),)

    role_id: Mapped[int] = mapped_column(ForeignKey("roles.id"), primary_key=True)
    permission_id: Mapped[int] = mapped_column(ForeignKey("permissions.id"), primary_key=True)


class LeadSource(Base):
    __tablename__ = "lead_sources"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(120), unique=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)


class BookingChannel(Base):
    __tablename__ = "booking_channels"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(120), unique=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)


class EmploymentType(Base):
    __tablename__ = "employment_types"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(120), unique=True)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)


class Client(Base, TimestampMixin):
    __tablename__ = "clients"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(160), index=True)
    company_name: Mapped[str | None] = mapped_column(String(160))
    client_type: Mapped[str] = mapped_column(String(40), default="Individual")
    phone: Mapped[str] = mapped_column(String(30), index=True)
    alternate_phone: Mapped[str | None] = mapped_column(String(30))
    email: Mapped[str | None] = mapped_column(String(160))
    address: Mapped[str | None] = mapped_column(Text)
    district: Mapped[str | None] = mapped_column(String(80))
    city: Mapped[str | None] = mapped_column(String(80))
    lead_source: Mapped[str | None] = mapped_column(String(80))
    booking_channel: Mapped[str | None] = mapped_column(String(80))
    first_enquiry_date: Mapped[date | None] = mapped_column(Date)
    last_enquiry_date: Mapped[date | None] = mapped_column(Date)
    status: Mapped[str] = mapped_column(String(40), default="Active")
    notes: Mapped[str | None] = mapped_column(Text)
    google_review_status: Mapped[str | None] = mapped_column(String(40))
    referral_generated: Mapped[bool] = mapped_column(Boolean, default=False)
    average_rating: Mapped[Decimal | None] = mapped_column(Numeric(4, 2))


class Enquiry(Base, TimestampMixin):
    __tablename__ = "enquiries"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"), index=True)
    client_name: Mapped[str] = mapped_column(String(160))
    phone: Mapped[str] = mapped_column(String(30))
    email: Mapped[str | None] = mapped_column(String(160))
    event_type: Mapped[str | None] = mapped_column(String(80))
    event_date: Mapped[date | None] = mapped_column(Date)
    start_time: Mapped[time | None] = mapped_column(Time)
    end_time: Mapped[time | None] = mapped_column(Time)
    location: Mapped[str | None] = mapped_column(String(160))
    district: Mapped[str | None] = mapped_column(String(80))
    city: Mapped[str | None] = mapped_column(String(80))
    venue: Mapped[str | None] = mapped_column(String(160))
    requirements: Mapped[str | None] = mapped_column(Text)
    expected_budget: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
    lead_source: Mapped[str | None] = mapped_column(String(80))
    booking_channel: Mapped[str | None] = mapped_column(String(80))
    assigned_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))
    status: Mapped[str] = mapped_column(String(40), default="New", index=True)

    client: Mapped[Client] = relationship()
    assigned_user: Mapped[User | None] = relationship()
    products: Mapped[list["EnquiryProduct"]] = relationship(cascade="all, delete-orphan")


class EnquiryProduct(Base):
    __tablename__ = "enquiry_products"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    enquiry_id: Mapped[int] = mapped_column(ForeignKey("enquiries.id"), index=True)
    product_id: Mapped[int | None] = mapped_column(ForeignKey("products.id"))
    name: Mapped[str] = mapped_column(String(160))
    quantity: Mapped[int] = mapped_column(Integer, default=1)
    unit_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)


class EnquiryFollowup(Base, TimestampMixin):
    __tablename__ = "enquiry_followups"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    enquiry_id: Mapped[int] = mapped_column(ForeignKey("enquiries.id"), index=True)
    assigned_user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))
    followup_date: Mapped[date] = mapped_column(Date)
    followup_time: Mapped[time | None] = mapped_column(Time)
    followup_type: Mapped[str] = mapped_column(String(40), default="Call")
    notes: Mapped[str | None] = mapped_column(Text)
    status: Mapped[str] = mapped_column(String(40), default="Pending")
    next_followup_date: Mapped[date | None] = mapped_column(Date)

    enquiry: Mapped[Enquiry] = relationship()
    assigned_user: Mapped[User | None] = relationship()


class Quotation(Base, TimestampMixin):
    __tablename__ = "quotations"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    enquiry_id: Mapped[int | None] = mapped_column(ForeignKey("enquiries.id"))
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"), index=True)
    event_id: Mapped[int | None] = mapped_column(Integer)
    quotation_date: Mapped[date] = mapped_column(Date)
    valid_until: Mapped[date | None] = mapped_column(Date)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    subtotal: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    final_amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    terms: Mapped[str | None] = mapped_column(Text)
    status: Mapped[str] = mapped_column(String(40), default="Draft")

    items: Mapped[list["QuotationItem"]] = relationship(cascade="all, delete-orphan")
    client: Mapped[Client] = relationship()


class QuotationItem(Base):
    __tablename__ = "quotation_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    quotation_id: Mapped[int] = mapped_column(ForeignKey("quotations.id"))
    product_id: Mapped[int | None] = mapped_column(ForeignKey("products.id"))
    name: Mapped[str] = mapped_column(String(160))
    quantity: Mapped[int] = mapped_column(Integer, default=1)
    unit_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)


class ProductCategory(Base):
    __tablename__ = "product_categories"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(120), unique=True)
    description: Mapped[str | None] = mapped_column(String(255))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)


class Product(Base, TimestampMixin):
    __tablename__ = "products"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    slug: Mapped[str | None] = mapped_column(String(180), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(160))
    category_id: Mapped[int | None] = mapped_column(ForeignKey("product_categories.id"))
    description: Mapped[str | None] = mapped_column(Text)
    base_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    cost: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    service_duration: Mapped[str | None] = mapped_column(String(80))
    image_url: Mapped[str | None] = mapped_column(String(500))
    video_url: Mapped[str | None] = mapped_column(String(500))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)

    category: Mapped[ProductCategory | None] = relationship()


class Event(Base, TimestampMixin):
    __tablename__ = "events"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(160))
    event_type: Mapped[str | None] = mapped_column(String(80))
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"), index=True)
    enquiry_id: Mapped[int | None] = mapped_column(ForeignKey("enquiries.id"))
    quotation_id: Mapped[int | None] = mapped_column(ForeignKey("quotations.id"))
    invoice_id: Mapped[int | None] = mapped_column(Integer)
    event_date: Mapped[date] = mapped_column(Date, index=True)
    district: Mapped[str | None] = mapped_column(String(80))
    city: Mapped[str | None] = mapped_column(String(80))
    venue_name: Mapped[str | None] = mapped_column(String(160))
    venue_address: Mapped[str | None] = mapped_column(Text)
    start_time: Mapped[time | None] = mapped_column(Time)
    end_time: Mapped[time | None] = mapped_column(Time)
    status: Mapped[str] = mapped_column(String(40), default="Confirmed", index=True)
    lead_source: Mapped[str | None] = mapped_column(String(80))
    booking_channel: Mapped[str | None] = mapped_column(String(80))
    created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))

    client: Mapped[Client] = relationship()
    products: Mapped[list["EventProduct"]] = relationship(cascade="all, delete-orphan")
    allocations: Mapped[list["StaffAllocation"]] = relationship(cascade="all, delete-orphan")


class EventProduct(Base):
    __tablename__ = "event_products"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    event_id: Mapped[int] = mapped_column(ForeignKey("events.id"))
    product_id: Mapped[int | None] = mapped_column(ForeignKey("products.id"))
    name: Mapped[str] = mapped_column(String(160))
    quantity: Mapped[int] = mapped_column(Integer, default=1)
    unit_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    cost: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)


class Employee(Base, TimestampMixin):
    __tablename__ = "employees"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    name: Mapped[str] = mapped_column(String(160), index=True)
    phone: Mapped[str] = mapped_column(String(30))
    email: Mapped[str | None] = mapped_column(String(160))
    role: Mapped[str] = mapped_column(String(80))
    department: Mapped[str | None] = mapped_column(String(80))
    joining_date: Mapped[date | None] = mapped_column(Date)
    employment_type: Mapped[str] = mapped_column(String(40), default="Regular")
    salary: Mapped[Decimal | None] = mapped_column(Numeric(12, 2))
    availability_status: Mapped[str] = mapped_column(String(40), default="Available")
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    address: Mapped[str | None] = mapped_column(Text)
    emergency_contact: Mapped[str | None] = mapped_column(String(80))


class StaffAllocation(Base, TimestampMixin):
    __tablename__ = "staff_allocations"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    event_id: Mapped[int] = mapped_column(ForeignKey("events.id"), index=True)
    employee_id: Mapped[int] = mapped_column(ForeignKey("employees.id"), index=True)
    assigned_role: Mapped[str] = mapped_column(String(80))
    allocation_date: Mapped[date] = mapped_column(Date)
    status: Mapped[str] = mapped_column(String(40), default="Assigned")
    event_payment: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    travel_allowance: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    other_allowance: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    total_staff_cost: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    manager_feedback: Mapped[str | None] = mapped_column(Text)
    performance_rating: Mapped[Decimal | None] = mapped_column(Numeric(4, 2))
    tasks_completed: Mapped[int] = mapped_column(Integer, default=0)

    event: Mapped[Event] = relationship(back_populates="allocations")
    employee: Mapped[Employee] = relationship()


class StaffPayment(Base, TimestampMixin):
    __tablename__ = "staff_payments"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    allocation_id: Mapped[int] = mapped_column(ForeignKey("staff_allocations.id"))
    employee_id: Mapped[int] = mapped_column(ForeignKey("employees.id"))
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    payment_date: Mapped[date] = mapped_column(Date)
    notes: Mapped[str | None] = mapped_column(Text)


class Invoice(Base, TimestampMixin):
    __tablename__ = "invoices"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    invoice_number: Mapped[str] = mapped_column(String(40), unique=True, index=True)
    event_id: Mapped[int | None] = mapped_column(ForeignKey("events.id"))
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"), index=True)
    invoice_date: Mapped[date] = mapped_column(Date)
    due_date: Mapped[date | None] = mapped_column(Date)
    subtotal: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    final_amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    amount_paid: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    balance_amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    payment_status: Mapped[str] = mapped_column(String(40), default="Unpaid")
    status: Mapped[str] = mapped_column(String(40), default="Draft")

    items: Mapped[list["InvoiceItem"]] = relationship(cascade="all, delete-orphan")
    client: Mapped[Client] = relationship()
    event: Mapped[Event | None] = relationship()


class InvoiceItem(Base):
    __tablename__ = "invoice_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    invoice_id: Mapped[int] = mapped_column(ForeignKey("invoices.id"))
    name: Mapped[str] = mapped_column(String(160))
    quantity: Mapped[int] = mapped_column(Integer, default=1)
    unit_price: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    discount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2), default=0)


class Payment(Base, TimestampMixin):
    __tablename__ = "payments"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    invoice_id: Mapped[int] = mapped_column(ForeignKey("invoices.id"), index=True)
    event_id: Mapped[int | None] = mapped_column(ForeignKey("events.id"))
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"))
    payment_date: Mapped[date] = mapped_column(Date)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2))
    payment_mode: Mapped[str] = mapped_column(String(40), default="UPI")
    transaction_reference: Mapped[str | None] = mapped_column(String(80))
    notes: Mapped[str | None] = mapped_column(Text)
    recorded_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))

    invoice: Mapped[Invoice] = relationship()
    client: Mapped[Client] = relationship()


class ExpenseCategory(Base):
    __tablename__ = "expense_categories"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(80), unique=True)


class Expense(Base, TimestampMixin):
    __tablename__ = "expenses"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    public_id: Mapped[str] = mapped_column(String(20), unique=True, index=True)
    event_id: Mapped[int | None] = mapped_column(ForeignKey("events.id"), index=True)
    category: Mapped[str] = mapped_column(String(80))
    description: Mapped[str | None] = mapped_column(Text)
    amount: Mapped[Decimal] = mapped_column(Numeric(12, 2))
    expense_date: Mapped[date] = mapped_column(Date)
    payment_mode: Mapped[str] = mapped_column(String(40), default="Cash")
    receipt_url: Mapped[str | None] = mapped_column(String(255))
    added_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))
    approval_status: Mapped[str] = mapped_column(String(40), default="Pending")
    notes: Mapped[str | None] = mapped_column(Text)

    event: Mapped[Event | None] = relationship()


class EnquiryForm(Base, TimestampMixin):
    __tablename__ = "forms"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String(160))
    description: Mapped[str | None] = mapped_column(Text)
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"))

    fields: Mapped[list["FormField"]] = relationship(cascade="all, delete-orphan")
    submissions: Mapped[list["FormSubmission"]] = relationship(cascade="all, delete-orphan")


class FormField(Base):
    __tablename__ = "form_fields"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    form_id: Mapped[int] = mapped_column(ForeignKey("forms.id"))
    label: Mapped[str] = mapped_column(String(160))
    field_key: Mapped[str] = mapped_column(String(80))
    field_type: Mapped[str] = mapped_column(String(40), default="text")
    is_required: Mapped[bool] = mapped_column(Boolean, default=False)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)

    options: Mapped[list["FormFieldOption"]] = relationship(cascade="all, delete-orphan")


class FormFieldOption(Base):
    __tablename__ = "form_field_options"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    field_id: Mapped[int] = mapped_column(ForeignKey("form_fields.id"))
    label: Mapped[str] = mapped_column(String(160))
    value: Mapped[str] = mapped_column(String(160))


class FormSubmission(Base, TimestampMixin):
    __tablename__ = "form_submissions"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    form_id: Mapped[int] = mapped_column(ForeignKey("forms.id"))
    payload: Mapped[str] = mapped_column(Text)
    enquiry_id: Mapped[int | None] = mapped_column(ForeignKey("enquiries.id"))
    client_id: Mapped[int | None] = mapped_column(ForeignKey("clients.id"))


class Feedback(Base, TimestampMixin):
    __tablename__ = "feedback"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    event_id: Mapped[int] = mapped_column(ForeignKey("events.id"))
    client_id: Mapped[int] = mapped_column(ForeignKey("clients.id"))
    rating: Mapped[Decimal | None] = mapped_column(Numeric(4, 2))
    google_review_given: Mapped[bool] = mapped_column(Boolean, default=False)
    notes: Mapped[str | None] = mapped_column(Text)


class Document(Base, TimestampMixin):
    __tablename__ = "documents"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    event_id: Mapped[int | None] = mapped_column(ForeignKey("events.id"))
    client_id: Mapped[int | None] = mapped_column(ForeignKey("clients.id"))
    title: Mapped[str] = mapped_column(String(160))
    file_url: Mapped[str] = mapped_column(String(255))
    doc_type: Mapped[str] = mapped_column(String(40), default="other")


class GalleryItem(Base, TimestampMixin):
    __tablename__ = "gallery_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    title: Mapped[str] = mapped_column(String(160))
    tag: Mapped[str] = mapped_column(String(80), default="Celebration")
    image_url: Mapped[str] = mapped_column(String(500))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    sort_order: Mapped[int] = mapped_column(Integer, default=0)


class CompanySetting(Base):
    __tablename__ = "company_settings"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    company_name: Mapped[str] = mapped_column(String(160), default="SelfiePetti")
    phone: Mapped[str | None] = mapped_column(String(30))
    email: Mapped[str | None] = mapped_column(String(160))
    address: Mapped[str | None] = mapped_column(Text)
    gstin: Mapped[str | None] = mapped_column(String(30))
    website: Mapped[str | None] = mapped_column(String(160))


class InvoiceSetting(Base):
    __tablename__ = "invoice_settings"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    prefix: Mapped[str] = mapped_column(String(20), default="INV")
    next_number: Mapped[int] = mapped_column(Integer, default=1)
    default_due_days: Mapped[int] = mapped_column(Integer, default=7)
    terms: Mapped[str | None] = mapped_column(Text)


class QuotationSetting(Base):
    __tablename__ = "quotation_settings"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    prefix: Mapped[str] = mapped_column(String(20), default="QUO")
    next_number: Mapped[int] = mapped_column(Integer, default=1)


class SystemSetting(Base):
    __tablename__ = "system_settings"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    currency: Mapped[str] = mapped_column(String(10), default="INR")
    timezone: Mapped[str] = mapped_column(String(60), default="Asia/Kolkata")
    date_format: Mapped[str] = mapped_column(String(20), default="dd MMM yyyy")
