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 { SocialMedias } from './social-medias.entity';
import { InfluencerCategory } from './influencer-caregories.entity';
import { BrandIndustries } from './brand-industries.entity';
import { Card } from './card.entity';
import { FacebookPages } from './facebook-pages.entity';
import { InstagramProfiles } from './instagram-profile.entity';
import { InstagramInsights } from './instagram-insights.entity';
import { TikTokProfiles } from './tiktok-profile.entity';
import { YoutubeChannels } from './youtube-channel.entity';
import { XiaohongshuProfiles } from './xiaohongshu-profile.entity';
import { SocialMediaPosts } from './social-media-posts.entity';
import { Subscription } from './subscription.entity';
import { BrandFavorite } from './brand-favorit.entity';
import { UserKeywords } from './user-keywords.entity';
import { Campaign } from './campaign.entity';
import { CampaignInfluencer } from './campaign_influencer.entity';
import { CampaignInfluencerBookmarks } from './campaign_influencer_bookmark.entity';
import { InfluenceSaveCampaigns } from './influencer-save-campaigns.entity';
import { BankDetails } from './bank-details.entity';
import { CampaignRecommended } from './campaign_recommended.entity';
import { InfluencerCampaignRecommended } from './influencer_campaign_recommended.entity';
import { UserPermissions } from './user-permissions.entity';
import { UserNotificationPreferences } from './user-notification-preferences.entity';
import { SearchLog } from './search-logs.entity';
import { InfluencerPaymentMethod } from './influencer-payment-method.entity';

export enum UserRoles {
  ADMIN = 'admin',
  BRAND = 'brand',
  INFLUENCER = 'influencer',
}

@Entity({ name: 'users' })
// @Unique(['email'])
@Index(['first_name', 'last_name'])
@Index('idx_users_role_active_followers', ['role', 'is_active', 'total_followers'], {
  where: `"deleted_at" IS NULL`,
})
@Index('idx_users_engagement_ratio', ['engagement_ratio'], {
  where: `"deleted_at" IS NULL`,
})
export class Users {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({
    type: 'enum',
    enum: UserRoles,
    default: UserRoles.BRAND,
  })
  role: string;

  @Column({ default: 'en' })
  language: string;

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

  @Column({ type: 'timestamp', nullable: true })
  otp_time: Date;

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

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

  @Column({ type: 'timestamp', nullable: true })
  whatsapp_request_at: Date;

  @Column({ type: 'boolean', nullable: true, comment: 'WhatsApp phone number verification status' })
  is_whatsapp_verified: boolean;

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

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

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

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

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

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

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

  @Column({ type: 'boolean', default: true })
  is_active: boolean;

  @Column({ default: 0, comment: 'Remaining registrations Process step by step' })
  pending_steps: number;

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

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

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

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

  @Column({ default: 0, comment: 'All platform Followers' })
  total_followers: number;

  @Column({ default: 0, comment: 'All platform Following' })
  total_following: number;

  @Column({ type: "decimal", precision: 10, scale: 4, default: 0.0000 })
  engagement_ratio: number

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

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

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

  @Column({ type: 'boolean', default: true })
  remember_me: boolean;

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

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

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

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

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

  @Column({ type: 'varchar', length: 255, nullable: true })
  offline_payment_allowed: string;

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

  @Column({ type: 'timestamp', nullable: true })
  recommended_date_time: Date;

  @Column({ type: 'boolean', default: true, comment: "Counts Column not updated FROM CRON(APIFY)" })
  is_updated_by_cron: boolean;

  @Column({ type: 'boolean', default: true })
  is_show_campaign_popup: boolean;

  @Column({ type: "decimal", precision: 10, scale: 4, default: 0.0000 })
  engagement_rate_overall: number;

  @Column({ type: "decimal", precision: 10, scale: 4, default: 0.0000 })
  engagement_rate_percentage: number;

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

  @Column({ type: 'timestamp', nullable: true })
  subscription_start_date: Date;

  @Column({ type: 'timestamp', nullable: true })
  subscription_end_date: Date;

  @Expose()
  favoritesCount: string;

  @Expose()
  isCampaignAdded: string;

  @Expose()
  isBookmarkAdded: string;

  @Expose()
  isArchived: string;

  @Expose()
  influencerUser: string;

  @Column({ type: 'boolean', default: false, comment: "Is trail used" })
  is_trial_used: boolean;

  @Column({ type: 'boolean', default: false, comment: "Is invoice sent" })
  is_invoice_sent: boolean;

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

  @OneToMany(() => SocialMedias, (media) => media.users)
  social_medias: SocialMedias[];

  @OneToMany(() => Card, (card) => card.users)
  cards: Card[];

  @OneToMany(() => InfluencerCategory, (category) => category.users)
  influencerCategories: InfluencerCategory[];

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

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

  @OneToMany(() => InstagramInsights, (insights) => insights.users)
  instagramInsights: InstagramInsights[];

  @OneToMany(() => YoutubeChannels, (youtube) => youtube.users)
  youtubeChannels: YoutubeChannels[];

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

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

  @OneToMany(() => BrandIndustries, (category) => category.users)
  brandIndustries: BrandIndustries[];

  @OneToMany(() => SocialMediaPosts, (posts) => posts.users)
  socialMediaPosts: SocialMediaPosts[];

  @OneToMany(() => Subscription, (sub) => sub.users)
  subscription: Subscription[];

  @OneToMany(() => BrandFavorite, (favorite) => favorite.brand)
  favorites: BrandFavorite[];

  @OneToMany(() => BrandFavorite, (favorite) => favorite.influencer)
  favoritedBy: BrandFavorite[];

  @OneToMany(() => UserKeywords, (keyword) => keyword.users)
  userKeywords: UserKeywords[];

  @OneToMany(() => Campaign, (campaign) => campaign.users)
  campaigns: Campaign[];

  @OneToMany(() => CampaignInfluencer, (campaign) => campaign.influencer)
  campaignInfluencer: CampaignInfluencer[];

  @OneToMany(() => CampaignInfluencerBookmarks, (campaign) => campaign.influencer)
  campaignBookmarks: CampaignInfluencerBookmarks[];

  @OneToMany(() => InfluenceSaveCampaigns, (campaign) => campaign.influencer)
  influenceSaveCampaigns: InfluenceSaveCampaigns[];

  @OneToMany(() => CampaignRecommended, (campaign) => campaign.influencer)
  campaignRecommend: CampaignRecommended[];

  @OneToMany(() => InfluencerCampaignRecommended, (campaign) => campaign.influencer)
  influencerCampaignRecommended: InfluencerCampaignRecommended[];

  @OneToMany(() => UserPermissions, (permission) => permission.users)
  permissions: UserPermissions[];

  @OneToOne(() => BankDetails, (bank) => bank.influencer)
  bankDetails: BankDetails;

  @OneToMany(() => SearchLog, log => log.user)
  searchLogs: SearchLog[];

  @OneToMany(() => InfluencerPaymentMethod, (influencerPaymentMethod) => influencerPaymentMethod.influencer)
  influencerPaymentMethods: InfluencerPaymentMethod[];

  @OneToMany(() => UserNotificationPreferences, (pref) => pref.user)
  notificationPreferences: UserNotificationPreferences[];

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