import type { Models } from "appwrite";

export const DATABASE_ID = "isletmem";

export const TABLES = {
  tenantSettings: "tenant_settings",
  customers: "customers",
  services: "services",
  software: "software",
  customerSoftware: "customer_software",
  calendarEvents: "calendar_events",
  tasks: "tasks",
  financeEntries: "finance_entries",
  invoices: "invoices",
  invoiceItems: "invoice_items",
  auditLogs: "audit_logs",
} as const;

export type TableId = (typeof TABLES)[keyof typeof TABLES];

type Row = Models.Document;

export type TenantRole = "owner" | "admin" | "member";

export interface TenantSettings extends Row {
  tenantId: string;
  companyName: string;
  companyTaxId?: string;
  companyAddress?: string;
  companyEmail?: string;
  companyPhone?: string;
  logo?: string;
  defaultVatRate?: number;
  invoicePrefix?: string;
  invoiceCounter?: number;
}

export type CustomerStatus = "active" | "passive";

export interface Customer extends Row {
  tenantId: string;
  createdBy: string;
  name: string;
  email?: string;
  phone?: string;
  taxId?: string;
  address?: string;
  notes?: string;
  status?: CustomerStatus;
}

export type BillingPeriod = "monthly" | "yearly" | "onetime";

export interface Service extends Row {
  tenantId: string;
  createdBy: string;
  customerId: string;
  name: string;
  description?: string;
  unitPrice: number;
  recurring?: boolean;
  billingPeriod?: BillingPeriod;
}

export interface Software extends Row {
  tenantId: string;
  createdBy: string;
  name: string;
  version?: string;
  description?: string;
  defaultFee?: number;
}

export interface CustomerSoftware extends Row {
  tenantId: string;
  createdBy: string;
  customerId: string;
  softwareId: string;
  startDate?: string;
  endDate?: string;
  fee?: number;
  billingPeriod?: BillingPeriod;
  notes?: string;
}

export interface CalendarEvent extends Row {
  tenantId: string;
  createdBy: string;
  title: string;
  description?: string;
  start: string;
  end: string;
  allDay?: boolean;
  customerId?: string;
  color?: string;
}

export type TaskStatus = "backlog" | "todo" | "in_progress" | "done";
export type TaskPriority = "low" | "medium" | "high" | "urgent";

export interface Task extends Row {
  tenantId: string;
  createdBy: string;
  title: string;
  description?: string;
  status?: TaskStatus;
  priority?: TaskPriority;
  dueDate?: string;
  assigneeId?: string;
  customerId?: string;
  order?: number;
}

export type FinanceType = "income" | "expense" | "debt" | "receivable";
export type PaymentMethod = "cash" | "transfer" | "card" | "check" | "other";

export interface FinanceEntry extends Row {
  tenantId: string;
  createdBy: string;
  type: FinanceType;
  amount: number;
  date: string;
  description?: string;
  customerId?: string;
  invoiceId?: string;
  paymentMethod?: PaymentMethod;
}

export type InvoiceStatus = "draft" | "sent" | "paid" | "overdue" | "cancelled";

export interface Invoice extends Row {
  tenantId: string;
  createdBy: string;
  number: string;
  customerId: string;
  issueDate: string;
  dueDate: string;
  status?: InvoiceStatus;
  subtotal?: number;
  vatTotal?: number;
  total?: number;
  notes?: string;
}

export interface InvoiceItem extends Row {
  tenantId: string;
  createdBy: string;
  invoiceId: string;
  description: string;
  quantity: number;
  unitPrice: number;
  vatRate?: number;
  lineTotal: number;
}

export type AuditAction = "create" | "update" | "delete";

export interface AuditLog extends Row {
  tenantId: string;
  userId: string;
  action: AuditAction;
  entityType: string;
  entityId: string;
  changes?: string;
  ipAddress?: string;
  userAgent?: string;
}
