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 { FacebookPosts } from './faceboook-posts.entity';

@Entity({ name: 'facebook_pages' })
export class FacebookPages {

  @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: "" })
  user_name: string;

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

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

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

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

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

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

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

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

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

  @Column({ type: 'text', default: "", comment: "Facebook page Id" })
  facebook_page_id: string;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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