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

/**
 * PartnerAddress Entity
 *
 * Synchronized with DDL: database/ddl/16-partners.sql
 * Table: partners.partner_addresses
 *
 * Represents addresses associated with a partner (billing, shipping, etc.).
 */
@Entity({ name: 'partner_addresses', schema: 'partners' })
export class PartnerAddress {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  // Direccion
  @Column({ name: 'address_line1', type: 'varchar', length: 200 })
  addressLine1: string;

  @Column({ name: 'address_line2', type: 'varchar', length: 200, nullable: true })
  addressLine2: string | null;

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

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

  @Column({ name: 'postal_code', type: 'varchar', length: 20, nullable: true })
  postalCode: string | null;

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

  // Contacto en esta direccion
  @Column({ name: 'contact_name', type: 'varchar', length: 100, nullable: true })
  contactName: string | null;

  @Column({ name: 'contact_phone', type: 'varchar', length: 30, nullable: true })
  contactPhone: string | null;

  @Column({ name: 'contact_email', type: 'varchar', length: 255, nullable: true })
  contactEmail: string | null;

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

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

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

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

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

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

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

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;
}
