/**
 * Carrier Profile ORM Entity (Infrastructure Layer)
 *
 * TypeORM entity for carrier (transporteur) profile persistence
 * Linked to users and organizations for B2B carrier portal
 */

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { UserOrmEntity } from './user.orm-entity';
import { OrganizationOrmEntity } from './organization.orm-entity';
import { CsvBookingOrmEntity } from './csv-booking.orm-entity';
import { CarrierActivityOrmEntity } from './carrier-activity.orm-entity';

@Entity('carrier_profiles')
@Index('idx_carrier_profiles_user_id', ['userId'])
@Index('idx_carrier_profiles_org_id', ['organizationId'])
@Index('idx_carrier_profiles_company_name', ['companyName'])
@Index('idx_carrier_profiles_is_active', ['isActive'])
@Index('idx_carrier_profiles_is_verified', ['isVerified'])
export class CarrierProfileOrmEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'user_id', type: 'uuid', unique: true })
  userId: string;

  @ManyToOne(() => UserOrmEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user: UserOrmEntity;

  @Column({ name: 'organization_id', type: 'uuid' })
  organizationId: string;

  @ManyToOne(() => OrganizationOrmEntity, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'organization_id' })
  organization: OrganizationOrmEntity;

  // Professional Information
  @Column({ name: 'company_name', type: 'varchar', length: 255 })
  companyName: string;

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

  @Column({ name: 'vat_number', type: 'varchar', length: 50, nullable: true })
  vatNumber: string | null;

  // Contact
  @Column({ type: 'varchar', length: 50, nullable: true })
  phone: string | null;

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

  // Address
  @Column({ name: 'street_address', type: 'text', nullable: true })
  streetAddress: string | null;

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

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

  @Column({ type: 'char', length: 2, nullable: true })
  country: string | null;

  // Statistics
  @Column({ name: 'total_bookings_accepted', type: 'int', default: 0 })
  totalBookingsAccepted: number;

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

  @Column({ name: 'acceptance_rate', type: 'decimal', precision: 5, scale: 2, default: 0 })
  acceptanceRate: number;

  @Column({ name: 'total_revenue_usd', type: 'decimal', precision: 15, scale: 2, default: 0 })
  totalRevenueUsd: number;

  @Column({ name: 'total_revenue_eur', type: 'decimal', precision: 15, scale: 2, default: 0 })
  totalRevenueEur: number;

  // Preferences
  @Column({ name: 'preferred_currency', type: 'varchar', length: 3, default: 'USD' })
  preferredCurrency: string;

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

  @Column({ name: 'auto_accept_enabled', type: 'boolean', default: false })
  autoAcceptEnabled: boolean;

  // Metadata
  @Column({ name: 'is_verified', type: 'boolean', default: false })
  isVerified: boolean;

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

  @Column({ name: 'last_login_at', type: 'timestamp', nullable: true })
  lastLoginAt: Date | null;

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

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

  // Relations
  @OneToMany(() => CsvBookingOrmEntity, booking => booking.carrierProfile)
  bookings: CsvBookingOrmEntity[];

  @OneToMany(() => CarrierActivityOrmEntity, activity => activity.carrierProfile)
  activities: CarrierActivityOrmEntity[];
}
