import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { UserOrmEntity } from './user.orm-entity';

/**
 * CSV Rate Config ORM Entity
 *
 * Stores configuration for CSV-based shipping rates
 * Maps company names to their CSV files
 */
@Entity('csv_rate_configs')
export class CsvRateConfigOrmEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'company_name', type: 'varchar', length: 255, unique: true })
  companyName: string;

  @Column({ name: 'csv_file_path', type: 'varchar', length: 500 })
  csvFilePath: string;

  @Column({
    name: 'type',
    type: 'varchar',
    length: 50,
    default: 'CSV_ONLY',
  })
  type: 'CSV_ONLY' | 'CSV_AND_API';

  @Column({ name: 'has_api', type: 'boolean', default: false })
  hasApi: boolean;

  @Column({ name: 'api_connector', type: 'varchar', length: 100, nullable: true })
  apiConnector: string | null;

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

  @Column({ name: 'uploaded_at', type: 'timestamp', default: () => 'NOW()' })
  uploadedAt: Date;

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

  @ManyToOne(() => UserOrmEntity, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'uploaded_by' })
  uploader: UserOrmEntity | null;

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

  @Column({ name: 'row_count', type: 'integer', nullable: true })
  rowCount: number | null;

  @Column({ name: 'metadata', type: 'jsonb', nullable: true })
  metadata: Record<string, any> | null;

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

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