import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { TenantAwareEntity } from '../../../shared/entities/tenant-aware.entity';
import { Brand } from './brand.entity';

@Entity('products', { schema: 'crm' })
@Index(['tenant_id', 'slug'], { unique: true })
export class Product extends TenantAwareEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('uuid')
  brand_id: string;

  @ManyToOne(() => Brand, (brand) => brand.products, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'brand_id' })
  brand: Brand;

  @Column({ length: 255 })
  name: string;

  @Column({ length: 100 })
  slug: string;

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

  @Column({ length: 100, nullable: true })
  sku: string | null;

  @Column({ length: 100, nullable: true })
  category: string | null;

  @Column({ type: 'decimal', precision: 10, scale: 2, nullable: true })
  price: number | null;

  @Column({ length: 3, default: 'USD' })
  currency: string;

  @Column({ type: 'jsonb', nullable: true })
  image_urls: string[] | null;

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

  @Column({ default: true })
  is_active: boolean;

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