import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Unique,
} from 'typeorm';
import { User } from './User.js';
import { PlatformAccount } from './PlatformAccount.js';

@Entity('analytics_data')
@Unique('uk_account_date', ['accountId', 'date'])
export class AnalyticsData {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ type: 'int', name: 'user_id' })
  userId!: number;

  @Column({ type: 'int', name: 'account_id' })
  accountId!: number;

  @Column({ type: 'date' })
  date!: string;

  @Column({ type: 'int', default: 0, name: 'fans_count' })
  fansCount!: number;

  @Column({ type: 'int', default: 0, name: 'fans_increase' })
  fansIncrease!: number;

  @Column({ type: 'int', default: 0, name: 'views_count' })
  viewsCount!: number;

  @Column({ type: 'int', default: 0, name: 'likes_count' })
  likesCount!: number;

  @Column({ type: 'int', default: 0, name: 'comments_count' })
  commentsCount!: number;

  @Column({ type: 'int', default: 0, name: 'shares_count' })
  sharesCount!: number;

  @Column({ type: 'decimal', precision: 10, scale: 2, default: 0 })
  income!: number;

  @CreateDateColumn({ name: 'created_at' })
  createdAt!: Date;

  // 关联
  @ManyToOne(() => User, (user) => user.analyticsData, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'user_id' })
  user!: User;

  @ManyToOne(() => PlatformAccount, (account) => account.analyticsData, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'account_id' })
  account!: PlatformAccount;
}
