import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Category } from '../../categories/entities/category.entity';

@Entity({ schema: 'pos_micro', name: 'products' })
export class Product {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id' })
  tenantId: string;

  @Column({ name: 'category_id', nullable: true })
  categoryId: string;

  @Column({ length: 50, nullable: true })
  sku: string;

  @Column({ length: 50, nullable: true })
  barcode: string;

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

  @Column({ type: 'decimal', precision: 12, scale: 2 })
  price: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0 })
  cost: number;

  @Column({ name: 'tax_included', default: true })
  taxIncluded: boolean;

  @Column({ name: 'track_stock', default: false })
  trackStock: boolean;

  @Column({ name: 'stock_quantity', type: 'decimal', precision: 12, scale: 3, default: 0 })
  stockQuantity: number;

  @Column({ name: 'low_stock_alert', default: 5 })
  lowStockAlert: number;

  @Column({ name: 'image_url', length: 500, nullable: true })
  imageUrl: string;

  @Column({ length: 7, nullable: true })
  color: string;

  @Column({ name: 'is_favorite', default: false })
  isFavorite: boolean;

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

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

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

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

  // Relations
  @ManyToOne(() => Category, (category) => category.products, { nullable: true })
  @JoinColumn({ name: 'category_id' })
  category: Category;
}
