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 { YoutubeChannels } from './youtube-channel.entity';
import { FacebookPages } from './facebook-pages.entity';
import { InstagramProfiles } from './instagram-profile.entity';
import { TikTokProfiles } from './tiktok-profile.entity';
import { XiaohongshuProfiles } from './xiaohongshu-profile.entity';
import { SocialMediaPosts } from './social-media-posts.entity';
import { InstagramMedias } from './instagram-medias.entity';
import { TikTokVideos } from './tiktok-videos.entity';
import { FacebookPosts } from './faceboook-posts.entity';
import { YoutubeVideos } from './youtube-videos.entity';

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

@Entity({ name: 'social_medias' })
@Index('idx_social_medias_user', ['users'], {
  where: `"deleted_at" IS NULL`
})
export class SocialMedias {

  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

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

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

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

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

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

  @Column({ type: 'jsonb', nullable: true, default: [] })
  multiple_names: Record<string, any>;

  @Column({ nullable: true })
  country: string;

  @Column({ nullable: true })
  source_posts_from: string;

  @Column({ type: 'boolean', default: false })
  is_new_permission: boolean;

  @Column({ type: 'jsonb', nullable: true, default: [] })
  missing_permissions: string[];

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

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

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

  @Column({
    type: 'decimal',
    precision: 8,
    scale: 2,
    default: 0.00
  })
  engagement_rate_percentage: number;

  @Column({ default: null })
  access_token_expire_at: string;

  @Column({ type: 'boolean', default: false })
  is_token_expire: boolean;

  @Column({ type: 'boolean', default: false })
  is_verified: boolean;

  @Column({ type: 'text', nullable: true, comment: "FACEBOOK PAGE ACCESS TOKEN IF ITS LINK WITH INSTAGRAM ONLY" })
  page_access_token: string;

  @Column({ default: null, comment: "FACEBOOK PAGE ACCESS EXPIRE AT IF ITS LINK WITH INSTAGRAM ONLY" })
  page_access_token_expire_at: string;

  @Column({ type: 'boolean', default: false, comment: "FACEBOOK PAGE ACCESS TOKEN EXPIRE IF ITS LINK WITH INSTAGRAM ONLY" })
  is_page_token_expire: boolean;

  @Column({ type: 'boolean', default: false, comment: "DATA UPDATED FROM APIFY" })
  is_updated: boolean;

  @Column({ type: 'timestamp', nullable: true, comment: "DATA UPDATED AR FROM APIFY" })
  updated_by_cron: Date;

  @Column({ type: 'timestamp', nullable: true, comment: "LAST INSTAGRAM INSIGHTS CRON EXECUTION DATE" })
  instagram_insights_synced_at: Date;

  @Column({ type: 'boolean', default: false, comment: "SOCIAL MEDIA NOT FOUND" })
  social_media_not_found: boolean;

  @Column({ type: 'int', default: 0, comment: "APIFY DATA UPDATED ATTEMPTS" })
  apify_data_updated_attempts: number;

  @Column({ nullable: true })
  display_name: string;

  @Column({ nullable: true })
  city_name: string;

  @Column({ nullable: true })
  country_name: string;

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

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

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

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

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

  @OneToMany(() => FacebookPosts, (facebookPosts) => facebookPosts.socialMedia)
  facebookPosts: FacebookPosts[];

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

  @OneToMany(() => InstagramMedias, (instagramMedias) => instagramMedias.socialMedia)
  instagramMedias: InstagramMedias[];

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

  @OneToMany(() => TikTokVideos, (tikTokVideos) => tikTokVideos.socialMedia)
  tikTokVideos: TikTokVideos[];

  @OneToMany(() => XiaohongshuProfiles, (xiaohongshuProfiles) => xiaohongshuProfiles.socialMedia)
  xiaohongshuProfiles: XiaohongshuProfiles[];

  @OneToMany(() => SocialMediaPosts, (post) => post.socialMedia)
  socialMediaPosts: SocialMediaPosts[];


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