export type InvoiceStatus = 'draft' | 'issued' | 'approved' | 'paid' | 'cancelled'

export interface InvoiceItemTemplate {
  id: string
  ownerScope: 'platform' | 'business'
  businessId: string | null
  title: string
  duration: string | null
  worktime: string | null
  description: string | null
  price: number
  discountedPrice: number | null
  sortOrder: number
  isActive: boolean
  createdAt: string
  updatedAt: string
}

export interface InvoiceItem {
  id: string
  invoiceId: string
  templateId: string | null
  title: string
  duration: string | null
  worktime: string | null
  description: string | null
  price: number
  discountedPrice: number | null
  sortOrder: number
}

export interface InvoiceKeyPoint {
  id: string
  text: string
  sortOrder: number
}

export interface InvoiceAccount {
  id: string
  bankName: string
  accountHolderName: string | null
  cardNumber: string | null
  iban: string | null
  sortOrder: number
}

export interface Invoice {
  id: string
  publicId: string
  businessId: string
  ownerScope: 'platform' | 'business'
  issuerBusinessId: string | null
  status: InvoiceStatus
  name: string | null
  topText: string | null
  notes: string | null
  invoiceTemplateId: string | null
  publicUrl: string | null
  issuedBy: string | null
  issuedAt: string
  createdAt: string
  updatedAt: string
  business?: {
    id: string
    name: string
    nameFa: string | null
  }
  issuer?: {
    id: string
    firstName: string | null
    lastName: string | null
  } | null
  items?: InvoiceItem[]
  keyPoints?: InvoiceKeyPoint[]
  accounts?: InvoiceAccount[]
  subtotal?: number
  total?: number
}

/** Public viewer payload (no notes / issuer / sequential id). */
export type PublicInvoice = {
  publicId: string
  status: InvoiceStatus
  name: string | null
  topText: string | null
  issuedAt: string
  business?: Invoice['business']
  items?: InvoiceItem[]
  keyPoints?: InvoiceKeyPoint[]
  accounts?: InvoiceAccount[]
  subtotal?: number
  total?: number
  publicUrl: string | null
}

export interface InvoiceItemInput {
  templateId?: string
  title: string
  duration?: string
  worktime?: string
  description?: string
  price: number
  discountedPrice?: number | null
}

export interface InvoiceKeyPointInput {
  text: string
}

export interface InvoiceAccountInput {
  bankName: string
  accountHolderName?: string
  cardNumber?: string
  iban?: string
}

export interface CreateInvoicePayload {
  items: InvoiceItemInput[]
  name?: string
  topText?: string
  notes?: string
  invoiceTemplateId?: string
  keyPoints?: InvoiceKeyPointInput[]
  accounts?: InvoiceAccountInput[]
  status?: InvoiceStatus
}

export type UpdateInvoicePayload = {
  items: InvoiceItemInput[]
  name?: string | null
  topText?: string | null
  notes?: string | null
  invoiceTemplateId?: string | null
  keyPoints?: InvoiceKeyPointInput[]
  accounts?: InvoiceAccountInput[]
}

export interface InvoiceTemplateItem {
  id: string
  itemTemplateId: string | null
  title: string
  duration: string | null
  worktime: string | null
  description: string | null
  price: number
  discountedPrice: number | null
  sortOrder: number
}

export interface InvoiceTemplate {
  id: string
  ownerScope: 'platform' | 'business'
  businessId: string | null
  name: string
  topText: string | null
  sortOrder: number
  isActive: boolean
  createdAt: string
  updatedAt: string
  items: InvoiceTemplateItem[]
  keyPoints: InvoiceKeyPoint[]
  accounts: InvoiceAccount[]
}

export interface InvoiceTemplateItemInput {
  itemTemplateId?: string
  title: string
  duration?: string
  worktime?: string
  description?: string
  price: number
  discountedPrice?: number | null
}

export interface CreateInvoiceTemplatePayload {
  name: string
  topText?: string
  items: InvoiceTemplateItemInput[]
  keyPoints?: InvoiceKeyPointInput[]
  accounts?: InvoiceAccountInput[]
  sortOrder?: number
}

export type UpdateInvoiceTemplatePayload = Partial<CreateInvoiceTemplatePayload> & {
  isActive?: boolean
}

export interface InvoiceItemTemplatesResponse {
  items: InvoiceItemTemplate[]
}

export interface InvoiceTemplatesListResponse {
  items: InvoiceTemplate[]
}

export interface InvoicesListResponse {
  items: Invoice[]
  page: number
  pageSize: number
  total: number
  totalPages: number
}

export interface CreateInvoiceItemTemplatePayload {
  title: string
  duration?: string
  worktime?: string
  description?: string
  price: number
  discountedPrice?: number | null
  sortOrder?: number
}

export type UpdateInvoiceItemTemplatePayload = Partial<CreateInvoiceItemTemplatePayload> & {
  isActive?: boolean
}

/** @deprecated use InvoiceItemTemplatesResponse */
export type InvoiceTemplatesResponse = InvoiceItemTemplatesResponse
