/**
 * NotificationTemplate + TemplateTranslation Entities
 * Template system with i18n support
 * Compatible with erp-core template.entity
 *
 * @module Notifications
 */

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

export type TemplateCategory = 'system' | 'marketing' | 'transactional' | 'alert';

@Entity({ name: 'notification_templates', schema: 'notifications' })
@Unique(['tenantId', 'code', 'channelType'])
export class NotificationTemplate {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ type: 'varchar', length: 100 })
  code: string;

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

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

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

  @Index()
  @Column({ name: 'channel_type', type: 'varchar', length: 30 })
  channelType: ChannelType;

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

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

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

  @Column({ name: 'available_variables', type: 'jsonb', default: [] })
  availableVariables: string[];

  @Column({ name: 'default_locale', type: 'varchar', length: 10, default: 'es-MX' })
  defaultLocale: string;

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

  @Column({ name: 'is_system', type: 'boolean', default: false })
  isSystem: boolean;

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

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

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

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

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

  @OneToMany(() => TemplateTranslation, (t) => t.template)
  translations: TemplateTranslation[];
}

@Entity({ name: 'template_translations', schema: 'notifications' })
@Unique(['templateId', 'locale'])
export class TemplateTranslation {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'template_id', type: 'uuid' })
  templateId: string;

  @Column({ type: 'varchar', length: 10 })
  locale: string;

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

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

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

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

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

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

  @ManyToOne(() => NotificationTemplate, (t) => t.translations, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'template_id' })
  template: NotificationTemplate;
}
