import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Partner } from './partner.entity';

@Entity({ name: 'partner_addresses', schema: 'partners' })
export class PartnerAddress {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'partner_id', type: 'uuid' })
  partnerId: string;

  @ManyToOne(() => Partner, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'partner_id' })
  partner: Partner;

  // Tipo de direccion
  @Index()
  @Column({ name: 'address_type', type: 'varchar', length: 20, default: 'billing' })
  addressType: 'billing' | 'shipping' | 'both';

  @Column({ name: 'is_default', type: 'boolean', default: false })
  isDefault: boolean;

  // Direccion
  @Column({ type: 'varchar', length: 100, nullable: true })
  label: string;

  @Column({ type: 'varchar', length: 200 })
  street: string;

  @Column({ name: 'exterior_number', type: 'varchar', length: 20, nullable: true })
  exteriorNumber: string;

  @Column({ name: 'interior_number', type: 'varchar', length: 20, nullable: true })
  interiorNumber: string;

  @Column({ type: 'varchar', length: 100, nullable: true })
  neighborhood: string;

  @Column({ type: 'varchar', length: 100 })
  city: string;

  @Column({ type: 'varchar', length: 100, nullable: true })
  municipality: string;

  @Column({ type: 'varchar', length: 100 })
  state: string;

  @Column({ name: 'postal_code', type: 'varchar', length: 10 })
  postalCode: string;

  @Column({ type: 'varchar', length: 3, default: 'MEX' })
  country: string;

  // Referencia
  @Column({ type: 'text', nullable: true })
  reference: string;

  // Geolocalizacion
  @Column({ type: 'decimal', precision: 10, scale: 8, nullable: true })
  latitude: number;

  @Column({ type: 'decimal', precision: 11, scale: 8, nullable: true })
  longitude: number;

  // Metadata
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

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