import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
  Unique,
} from 'typeorm';
import { WhatsAppAccount } from './account.entity';

export type TemplateCategory = 'MARKETING' | 'UTILITY' | 'AUTHENTICATION';
export type TemplateStatus = 'PENDING' | 'APPROVED' | 'REJECTED' | 'PAUSED' | 'DISABLED';
export type HeaderType = 'TEXT' | 'IMAGE' | 'VIDEO' | 'DOCUMENT';

@Entity({ name: 'templates', schema: 'whatsapp' })
@Unique(['accountId', 'name', 'language'])
export class WhatsAppTemplate {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'account_id', type: 'uuid' })
  accountId: string;

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

  @Column({ name: 'name', type: 'varchar', length: 512 })
  name: string;

  @Column({ name: 'display_name', type: 'varchar', length: 200 })
  displayName: string;

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

  @Index()
  @Column({ name: 'category', type: 'varchar', length: 30 })
  category: TemplateCategory;

  @Column({ name: 'language', type: 'varchar', length: 10, default: 'es_MX' })
  language: string;

  @Column({ name: 'header_type', type: 'varchar', length: 20, nullable: true })
  headerType: HeaderType;

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

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

  @Column({ name: 'body_text', type: 'text' })
  bodyText: string;

  @Column({ name: 'body_variables', type: 'text', array: true, default: [] })
  bodyVariables: string[];

  @Column({ name: 'footer_text', type: 'varchar', length: 60, nullable: true })
  footerText: string;

  @Column({ name: 'buttons', type: 'jsonb', default: [] })
  buttons: Record<string, any>[];

  @Column({ name: 'meta_template_id', type: 'varchar', length: 50, nullable: true })
  metaTemplateId: string;

  @Index()
  @Column({ name: 'meta_status', type: 'varchar', length: 20, default: 'PENDING' })
  metaStatus: TemplateStatus;

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

  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @Column({ name: 'usage_count', type: 'int', default: 0 })
  usageCount: number;

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

  @Column({ name: 'version', type: 'int', default: 1 })
  version: number;

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

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

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

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

  @ManyToOne(() => WhatsAppAccount, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'account_id' })
  account: WhatsAppAccount;
}
