import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  OneToMany,
} from 'typeorm';
import { Company } from './company.entity.js';
import { User } from './user.entity.js';
import { Role } from './role.entity.js';

export enum TenantStatus {
  ACTIVE = 'active',
  SUSPENDED = 'suspended',
  TRIAL = 'trial',
  CANCELLED = 'cancelled',
}

export enum TenantPlan {
  BASIC = 'basic',
  STANDARD = 'standard',
  PREMIUM = 'premium',
  ENTERPRISE = 'enterprise',
}

@Entity({ schema: 'auth', name: 'tenants' })
@Index('idx_tenants_subdomain', ['subdomain'])
@Index('idx_tenants_status', ['status'], { where: 'deleted_at IS NULL' })
@Index('idx_tenants_created_at', ['createdAt'])
@Index('idx_tenants_plan', ['plan'])
export class Tenant {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'varchar', length: 100, unique: true, nullable: false })
  subdomain: string;

  @Column({
    type: 'varchar',
    length: 100,
    unique: true,
    nullable: false,
    name: 'schema_name',
  })
  schemaName: string;

  @Column({
    type: 'enum',
    enum: TenantStatus,
    default: TenantStatus.ACTIVE,
    nullable: false,
  })
  status: TenantStatus;

  @Column({
    type: 'enum',
    enum: TenantPlan,
    default: TenantPlan.BASIC,
    nullable: false,
  })
  plan: TenantPlan;

  @Column({ type: 'integer', default: 10, name: 'max_users' })
  maxUsers: number;

  @Column({ type: 'integer', default: 1024, name: 'max_storage_mb' })
  maxStorageMb: number;

  @Column({ type: 'integer', default: 0, name: 'current_storage_mb' })
  currentStorageMb: number;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'custom_domain' })
  customDomain: string | null;

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'contact_email' })
  contactEmail: string | null;

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

  @Column({ type: 'varchar', length: 255, nullable: true, name: 'billing_email' })
  billingEmail: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'tax_id' })
  taxId: string | null;

  @Column({ type: 'timestamp', nullable: true, name: 'trial_ends_at' })
  trialEndsAt: Date | null;

  @Column({ type: 'timestamp', nullable: true, name: 'subscription_ends_at' })
  subscriptionEndsAt: Date | null;

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

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

  // Relaciones
  @OneToMany(() => Company, (company) => company.tenant)
  companies: Company[];

  @OneToMany(() => User, (user) => user.tenant)
  users: User[];

  @OneToMany(() => Role, (role) => role.tenant)
  roles: Role[];

  // Auditoría
  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt: Date;

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

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp',
    nullable: true,
  })
  updatedAt: Date | null;

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

  @Column({ type: 'timestamp', nullable: true, name: 'deleted_at' })
  deletedAt: Date | null;

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