import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  Index,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { TenantAwareEntity } from '@/shared/entities/tenant-aware.entity';
import { Project } from './project.entity';
import { User } from '@/modules/auth/entities/user.entity';

export enum ContentType {
  SOCIAL_POST = 'social_post',
  BLOG_ARTICLE = 'blog_article',
  EMAIL = 'email',
  AD_COPY = 'ad_copy',
  LANDING_PAGE = 'landing_page',
  VIDEO_SCRIPT = 'video_script',
  PRODUCT_DESCRIPTION = 'product_description',
  OTHER = 'other',
}

export enum ContentStatus {
  IDEA = 'idea',
  DRAFTING = 'drafting',
  REVIEW = 'review',
  APPROVED = 'approved',
  SCHEDULED = 'scheduled',
  PUBLISHED = 'published',
  ARCHIVED = 'archived',
}

export enum SocialPlatform {
  FACEBOOK = 'facebook',
  INSTAGRAM = 'instagram',
  TWITTER = 'twitter',
  LINKEDIN = 'linkedin',
  TIKTOK = 'tiktok',
  YOUTUBE = 'youtube',
  PINTEREST = 'pinterest',
}

@Entity('content_pieces', { schema: 'projects' })
@Index(['tenant_id', 'project_id'])
@Index(['tenant_id', 'type'])
@Index(['tenant_id', 'status'])
export class ContentPiece extends TenantAwareEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('uuid')
  project_id: string;

  @ManyToOne(() => Project, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'project_id' })
  project: Project;

  @Column('uuid', { nullable: true })
  assigned_to: string | null;

  @ManyToOne(() => User, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'assigned_to' })
  assignee: User;

  @Column({ length: 255 })
  title: string;

  @Column({ type: 'enum', enum: ContentType, default: ContentType.SOCIAL_POST })
  type: ContentType;

  @Column({ type: 'enum', enum: ContentStatus, default: ContentStatus.IDEA })
  status: ContentStatus;

  @Column('simple-array', { nullable: true })
  platforms: SocialPlatform[] | null;

  @Column({ type: 'text', nullable: true })
  content: string | null;

  @Column({ type: 'text', nullable: true })
  content_html: string | null;

  @Column({ type: 'text', nullable: true })
  prompt_used: string | null;

  @Column({ type: 'jsonb', nullable: true })
  ai_metadata: Record<string, any> | null; // Model, tokens, etc.

  @Column('simple-array', { nullable: true })
  asset_ids: string[] | null;

  @Column({ type: 'text', nullable: true })
  call_to_action: string | null;

  @Column('simple-array', { nullable: true })
  hashtags: string[] | null;

  @Column({ type: 'timestamptz', nullable: true })
  scheduled_at: Date | null;

  @Column({ type: 'timestamptz', nullable: true })
  published_at: Date | null;

  @Column({ type: 'text', nullable: true })
  published_url: string | null;

  @Column({ type: 'int', default: 0 })
  version: number;

  @Column('simple-array', { nullable: true })
  tags: string[] | null;

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, any> | null;

  @CreateDateColumn({ type: 'timestamptz' })
  created_at: Date;

  @UpdateDateColumn({ type: 'timestamptz' })
  updated_at: Date;

  @Column({ type: 'timestamptz', nullable: true })
  deleted_at: Date | null;
}
