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

export type SourceType = 'third_party' | 'self_owned';

@Entity('categories')
@Index('idx_categories_source_provider_external', [
  'sourceType',
  'provider',
  'externalId',
])
@Index('idx_categories_provider_remote', ['provider', 'remoteCategoryId'])
export class Category {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

  @Column({ name: 'source_type', type: 'varchar', length: 32 })
  sourceType: SourceType;

  @Column({ name: 'api_account_id', type: 'varchar', length: 36, nullable: true })
  apiAccountId: string | null;

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

  @Column({ name: 'external_id', type: 'varchar', length: 160, nullable: true })
  externalId: string | null;

  @Column({ name: 'remote_category_id', type: 'varchar', length: 120, nullable: true })
  remoteCategoryId: string | null;

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

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

  @Column({ type: 'tinyint', default: 1 })
  enabled: boolean;

  @Column({ name: 'raw_payload', type: 'json', nullable: true })
  rawPayload: Record<string, unknown> | null;

  @Column({ name: 'last_synced_at', type: 'datetime', nullable: true })
  lastSyncedAt: Date | null;

  @CreateDateColumn({ name: 'created_at', type: 'datetime', precision: 6 })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'datetime', precision: 6 })
  updatedAt: Date;
}
