import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany, CreateDateColumn, UpdateDateColumn, DeleteDateColumn, ManyToMany, JoinTable, ManyToOne, Unique, Index } from 'typeorm';
import { v4 as uuidv4 } from 'uuid';
import { Exclude, Expose } from 'class-transformer';
import { Users } from './users.entity';
import { CountryListInstance } from 'twilio/lib/rest/pricing/v2/voice/country';
import { SocialMedias } from './social-medias.entity';
import { YoutubeVideos } from './youtube-videos.entity';

@Entity({ name: 'youtube_channels' })
export class YoutubeChannels {

  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToOne(() => Users, user => user.id, { nullable: true })
  @JoinColumn({ name: "users" })
  users: Users;

  @ManyToOne(() => SocialMedias, (socialMedia) => socialMedia.id)
  @JoinColumn({ name: "socialMedia" })
  socialMedia: SocialMedias;

  @Index()
  @Column({ default: "" })
  title: string;

  @Column({ type: 'text', default: "" })
  description: string;

  @Index()
  @Column({ type: 'text', default: "" })
  channel_name: string;

  @Column({ type: 'text', default: "" })
  published_at: string;

  @Column({ type: 'text', default: "" })
  thumbnails: string;

  @Column({ type: 'text', default: "" })
  localized: string;

  @Column({ type: 'text', default: "", comment: "Youtube Videos playlist Id" })
  content_id: string;

  @Column({ type: 'bigint', default: 0 })
  view_counts: string;

  @Column({ type: 'bigint', default: 0 })
  subscriber_count: string;

  @Column({ type: 'bigint', default: 0 })
  video_count: string;

  @Column({ type: 'bigint', default: 0 })
  engagement_count: string;

  @Column({ type: 'text', default: "" })
  url: string;

  @Column({
    type: 'decimal',
    precision: 8,
    scale: 4,
    default: 0.0000
  })
  engagement_rate: number;

  @Index()
  @Column({ default: "" })
  country_name: string;

  @Index()
  @Column({ default: "" })
  city_name: string;

  @Index()
  @Column({ default: "" })
  gender: string;

  @OneToMany(() => YoutubeVideos, (youtubeVideos) => youtubeVideos.youtubeChannel)
  youtubeVideos: YoutubeVideos[];

  @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();
  }
}