import {
  Entity,
  PrimaryGeneratedColumn,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
  DeleteDateColumn,
  Unique,
} from 'typeorm';
import { v4 as uuidv4 } from 'uuid';
import { Exclude } from 'class-transformer';
import { Countries } from './countries.entity';
import { PaymentMethod } from './payment-method.entity';

@Entity({ name: 'country_payment_methods' })
@Unique(['country', 'payment_method'])
export class CountryPaymentMethod {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToOne(() => Countries, (country) => country.id, { nullable: false, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'country_id' })
  country: Countries;

  @ManyToOne(() => PaymentMethod, (paymentMethod) => paymentMethod.id, { nullable: false, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'payment_method_id' })
  payment_method: PaymentMethod;

  @Exclude()
  @CreateDateColumn({
    type: 'timestamp',
    precision: 0,
    default: () => 'CURRENT_TIMESTAMP',
  })
  created_at: Date;

  @Exclude()
  @UpdateDateColumn({
    type: 'timestamp',
    precision: 0,
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updated_at: Date;

  @Exclude()
  @DeleteDateColumn({ type: 'timestamp', nullable: true, precision: 0 })
  deleted_at: Date;

  setDefaults() {
    this.id = this.id || uuidv4();
  }
}
