import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  Unique,
} from 'typeorm';

export type AccountStatus = 'pending' | 'active' | 'suspended' | 'disconnected';

@Entity({ name: 'accounts', schema: 'whatsapp' })
@Unique(['tenantId', 'phoneNumber'])
export class WhatsAppAccount {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Index()
  @Column({ name: 'phone_number', type: 'varchar', length: 20 })
  phoneNumber: string;

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

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

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

  @Column({ name: 'webhook_verify_token', type: 'varchar', length: 255, nullable: true })
  webhookVerifyToken: string;

  @Column({ name: 'webhook_secret', type: 'varchar', length: 255, nullable: true })
  webhookSecret: string;

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

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

  @Column({ name: 'business_category', type: 'varchar', length: 100, nullable: true })
  businessCategory: string;

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

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

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

  @Column({ name: 'auto_reply_enabled', type: 'boolean', default: false })
  autoReplyEnabled: boolean;

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

  @Column({ name: 'business_hours', type: 'jsonb', default: {} })
  businessHours: Record<string, any>;

  @Index()
  @Column({ name: 'status', type: 'varchar', length: 20, default: 'pending' })
  status: AccountStatus;

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

  @Column({ name: 'daily_message_limit', type: 'int', default: 1000 })
  dailyMessageLimit: number;

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

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

  @Column({ name: 'total_messages_sent', type: 'bigint', default: 0 })
  totalMessagesSent: number;

  @Column({ name: 'total_messages_received', type: 'bigint', default: 0 })
  totalMessagesReceived: 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;
}
