/**
 * PostSaleTicket Entity
 * Tickets de garantía postventa
 *
 * @module Quality
 * @table quality.post_sale_tickets
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { TicketAssignment } from './ticket-assignment.entity';

export type TicketPriority = 'urgent' | 'high' | 'medium' | 'low';
export type TicketStatus = 'created' | 'assigned' | 'in_progress' | 'resolved' | 'closed' | 'cancelled';
export type TicketCategory = 'plumbing' | 'electrical' | 'finishes' | 'carpentry' | 'structural' | 'other';

@Entity({ schema: 'construction', name: 'tickets_postventa' })
@Index(['tenantId', 'ticketNumber'], { unique: true })
export class PostSaleTicket {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'lote_id', type: 'uuid' })
  loteId: string;

  @Column({ name: 'derechohabiente_id', type: 'uuid', nullable: true })
  derechohabienteId: string;

  @Column({ name: 'ticket_number', type: 'varchar', length: 50 })
  ticketNumber: string;

  @Column({ type: 'varchar', length: 30 })
  category: TicketCategory;

  @Column({ type: 'varchar', length: 20 })
  priority: TicketPriority;

  @Column({ type: 'varchar', length: 255 })
  title: string;

  @Column({ type: 'text' })
  description: string;

  @Column({ name: 'photo_url', type: 'varchar', length: 500, nullable: true })
  photoUrl: string;

  @Column({ type: 'varchar', length: 20, default: 'created' })
  status: TicketStatus;

  @Column({ name: 'sla_hours', type: 'integer' })
  slaHours: number;

  @Column({ name: 'sla_due_at', type: 'timestamptz' })
  slaDueAt: Date;

  @Column({ name: 'sla_breached', type: 'boolean', default: false })
  slaBreached: boolean;

  @Column({ name: 'assigned_at', type: 'timestamptz', nullable: true })
  assignedAt: Date;

  @Column({ name: 'resolved_at', type: 'timestamptz', nullable: true })
  resolvedAt: Date;

  @Column({ name: 'closed_at', type: 'timestamptz', nullable: true })
  closedAt: Date;

  @Column({ name: 'resolution_notes', type: 'text', nullable: true })
  resolutionNotes: string;

  @Column({ name: 'resolution_photo_url', type: 'varchar', length: 500, nullable: true })
  resolutionPhotoUrl: string;

  @Column({ name: 'satisfaction_rating', type: 'integer', nullable: true })
  satisfactionRating: number;

  @Column({ name: 'satisfaction_comment', type: 'text', nullable: true })
  satisfactionComment: string;

  @Column({ name: 'contact_name', type: 'varchar', length: 200, nullable: true })
  contactName: string;

  @Column({ name: 'contact_phone', type: 'varchar', length: 20, nullable: true })
  contactPhone: string;

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

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

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

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedById: string;

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User;

  @OneToMany(() => TicketAssignment, (ta) => ta.ticket)
  assignments: TicketAssignment[];
}
