/**
 * Quote Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents service quotations for customers.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Diagnostic } from './diagnostic.entity';
import { ServiceOrder } from './service-order.entity';

export enum QuoteStatus {
  DRAFT = 'draft',
  SENT = 'sent',
  VIEWED = 'viewed',
  APPROVED = 'approved',
  REJECTED = 'rejected',
  EXPIRED = 'expired',
  CONVERTED = 'converted',
}

@Entity({ name: 'quotes', schema: 'service_management' })
@Index('idx_quotes_tenant', ['tenantId'])
@Index('idx_quotes_status', ['tenantId', 'status'])
@Index('idx_quotes_customer', ['customerId'])
export class Quote {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'quote_number', type: 'varchar', length: 20 })
  quoteNumber: string;

  @Column({ name: 'customer_id', type: 'uuid' })
  customerId: string;

  @Column({ name: 'vehicle_id', type: 'uuid' })
  vehicleId: string;

  @Column({ name: 'diagnostic_id', type: 'uuid', nullable: true })
  diagnosticId?: string;

  @Column({
    type: 'varchar',
    length: 20,
    default: QuoteStatus.DRAFT,
  })
  status: QuoteStatus;

  // Dates
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @Column({ name: 'sent_at', type: 'timestamptz', nullable: true })
  sentAt?: Date;

  @Column({ name: 'viewed_at', type: 'timestamptz', nullable: true })
  viewedAt?: Date;

  @Column({ name: 'responded_at', type: 'timestamptz', nullable: true })
  respondedAt?: Date;

  @Column({ name: 'expires_at', type: 'timestamptz', nullable: true })
  expiresAt?: Date;

  // Totals
  @Column({ name: 'labor_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  laborTotal: number;

  @Column({ name: 'parts_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  partsTotal: number;

  @Column({ name: 'discount_amount', type: 'decimal', precision: 12, scale: 2, default: 0 })
  discountAmount: number;

  @Column({ name: 'discount_percent', type: 'decimal', precision: 5, scale: 2, default: 0 })
  discountPercent: number;

  @Column({ name: 'discount_reason', type: 'varchar', length: 200, nullable: true })
  discountReason?: string;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  tax: number;

  @Column({ name: 'grand_total', type: 'decimal', precision: 12, scale: 2, default: 0 })
  grandTotal: number;

  @Column({ name: 'validity_days', type: 'integer', default: 15 })
  validityDays: number;

  @Column({ type: 'text', nullable: true })
  terms?: string;

  @Column({ type: 'text', nullable: true })
  notes?: string;

  // Conversion to order
  @Column({ name: 'converted_order_id', type: 'uuid', nullable: true })
  convertedOrderId?: string;

  // Digital approval
  @Column({ name: 'approved_by_name', type: 'varchar', length: 200, nullable: true })
  approvedByName?: string;

  @Column({ name: 'approval_signature', type: 'text', nullable: true })
  approvalSignature?: string;

  @Column({ name: 'approval_ip', type: 'inet', nullable: true })
  approvalIp?: string;

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  // Relations
  @ManyToOne(() => Diagnostic, { nullable: true })
  @JoinColumn({ name: 'diagnostic_id' })
  diagnostic?: Diagnostic;

  @ManyToOne(() => ServiceOrder, { nullable: true })
  @JoinColumn({ name: 'converted_order_id' })
  convertedOrder?: ServiceOrder;

  // @OneToMany(() => QuoteItem, item => item.quote)
  // items: QuoteItem[];
}
