import {
  Column,
  CreateDateColumn,
  Entity,
  Index,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';
import type { SourceType } from './category.entity';

export type ProductSyncStatus = 'active' | 'removed';

@Entity('courses')
@Index('idx_courses_source_provider_external_category', [
  'sourceType',
  'provider',
  'externalId',
  'remoteCategoryId',
])
@Index('idx_courses_provider_category_updated', [
  'provider',
  'remoteCategoryId',
  'updatedAt',
])
export class Course {
  @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: 'category_id', type: 'varchar', length: 36, nullable: true })
  categoryId: string | null;

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

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

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

  @Column({ type: 'decimal', precision: 10, scale: 2, default: 0 })
  price: string;

  @Column({ name: 'cover_url', type: 'varchar', length: 500, nullable: true })
  coverUrl: string | null;

  @Column({ type: 'text', nullable: true })
  content: string | null;

  @Column({ type: 'int', nullable: true })
  stock: number | null;

  @Column({ name: 'sales_limit', type: 'int', nullable: true })
  salesLimit: number | null;

  @Column({ name: 'fulfillment_type', type: 'varchar', length: 40, default: 'third_party_api' })
  fulfillmentType: string;

  @Column({ name: 'after_sales_note', type: 'text', nullable: true })
  afterSalesNote: string | null;

  @Column({ name: 'order_form_schema', type: 'json', nullable: true })
  orderFormSchema: OrderFormField[] | null;

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

  @Column({ name: 'is_favorite', type: 'tinyint', default: 0 })
  isFavorite: boolean;

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

  @Column({ name: 'sync_status', type: 'varchar', length: 32, default: 'active' })
  syncStatus: ProductSyncStatus;

  @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;

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

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

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

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

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

export type OrderFormFieldType =
  | 'text'
  | 'password'
  | 'phone'
  | 'number'
  | 'textarea'
  | 'select'
  | 'checkbox';

export type OrderFormField = {
  key: string;
  label: string;
  type: OrderFormFieldType;
  required: boolean;
  placeholder?: string | null;
  options?: Array<{ label: string; value: string }>;
};
