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

export enum ResetPeriod {
  NONE = 'none',
  YEAR = 'year',
  MONTH = 'month',
}

@Entity({ schema: 'core', name: 'sequences' })
@Index('idx_sequences_tenant_id', ['tenantId'])
@Index('idx_sequences_code_tenant', ['tenantId', 'code'], { unique: true })
@Index('idx_sequences_active', ['tenantId', 'isActive'])
export class Sequence {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

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

  @Column({ type: 'integer', nullable: false, default: 1, name: 'next_number' })
  nextNumber: number;

  @Column({ type: 'integer', nullable: false, default: 4 })
  padding: number;

  @Column({
    type: 'enum',
    enum: ResetPeriod,
    nullable: true,
    default: ResetPeriod.NONE,
    name: 'reset_period',
  })
  resetPeriod: ResetPeriod | null;

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

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

  // Audit fields
  @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;
}
