import { Entity, Column, PrimaryGeneratedColumn, OneToOne, JoinColumn, OneToMany, CreateDateColumn, UpdateDateColumn, DeleteDateColumn, ManyToMany, JoinTable, ManyToOne, Unique } from 'typeorm';
import { v4 as uuidv4 } from 'uuid';
import { Exclude, Expose } from 'class-transformer';
import { Users } from './users.entity';
import { YoutubeChannels } from './youtube-channel.entity';
import { FacebookPages } from './facebook-pages.entity';
import { InstagramProfiles } from './instagram-profile.entity';
import { TikTokProfiles } from './tiktok-profile.entity';
import { SocialMedias } from './social-medias.entity';

export enum SocialType {
  YOUTUBE = 'youtube',
  FACEBOOK = 'facebook',
  INSTAGRAM = 'instagram',
  TIKTOK = 'tiktok',
}

@Entity({ name: 'social_media_posts' })
export class SocialMediaPosts {

  @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;

  @Column({
    type: 'enum',
    enum: SocialType,
    default: SocialType.YOUTUBE,
  })
  social_type: string;

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

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

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

  @Column({ type: 'text', nullable: true })
  post_url: string;

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

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

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

  @Column({ type: 'boolean', nullable: true })
  is_uploaded_on_s3: boolean;

  @Column({ default: "" })
  created_time: string;

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

  @OneToMany(() => YoutubeChannels, (youtubeChannel) => youtubeChannel.socialMedia)
  youtubeChannels: YoutubeChannels[];

  @OneToMany(() => FacebookPages, (facebookPages) => facebookPages.socialMedia)
  facebookPages: FacebookPages[];

  @OneToMany(() => InstagramProfiles, (instagramProfiles) => instagramProfiles.socialMedia)
  instagramProfiles: InstagramProfiles[];

  @OneToMany(() => TikTokProfiles, (tikTokProfiles) => tikTokProfiles.socialMedia)
  tikTokProfiles: TikTokProfiles[];

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