export type ProjectStatus = "active" | "paused";

export interface Project {
  id: string;
  name: string;
  website: string;
  industry: string;
  timezone: string;
  countries: string[];
  status: ProjectStatus;
  createdAt: string;
  updatedAt: string;
}

/** @deprecated Use Project — kept for gradual migration in UI shims */
export interface ProjectContext {
  id?: string;
  name: string;
  website: string;
  industry: string;
  timezone: string;
  countries: string[];
  status: ProjectStatus;
  created: boolean;
}

export interface BrandProfile {
  companyName: string;
  tagline: string;
  industry: string;
  brandVoice: string;
  logoDataUrl: string | null;
  logoFileName: string | null;
  /** Optional public contact phone shown on flyer creatives. */
  contactPhone?: string;
  /** Optional public contact email shown on flyer creatives when set. */
  contactEmail?: string;
}

export interface PostSequenceItem {
  id: string;
  title: string;
  summary: string;
  tag: string;
  content: string;
}

export interface PostingContextData {
  name: string;
  posts: PostSequenceItem[];
  saved: boolean;
}

export type GeneratedPostStatus = "draft" | "approved" | "published" | "rejected";

export type ConnectionStatus = "connected" | "disconnected" | "expired" | "error";

export type VideoGenerationStatus = "pending" | "generated" | "failed" | "skipped";

import type { AiUsageSummary } from "@/lib/ai/cost-tracking";
import type { BannerLayoutId } from "@/lib/generation/banner-layouts";

export interface GeneratedPost {
  id: string;
  userId?: string;
  projectId: string;
  projectName: string;
  contextName: string;
  sequencePostId: string;
  sequenceIndex: number;
  parentPostId?: string | null;
  version: number;
  title: string;
  caption: string;
  imageUrl: string | null;
  imagePrompt: string;
  imageApproved?: boolean;
  imageProvider?: string;
  imageProviderChain?: string;
  imageWarning?: string | null;
  videoUrl?: string | null;
  videoPrompt?: string;
  videoStoryboard?: string;
  videoStatus?: VideoGenerationStatus;
  videoDurationSeconds?: number;
  videoResolution?: string;
  videoWarning?: string | null;
  platforms: string[];
  status: GeneratedPostStatus;
  provider: string;
  model: string;
  contextSnapshot: string;
  engagementScore: number | null;
  /** Estimated AI API spend for this post (caption + brief + image). */
  aiCostUsd?: number;
  aiUsage?: AiUsageSummary;
  /** Multi-score QA report from generation governance. */
  qualityReport?: GenerationQaReportSnapshot | null;
  /** Unique media filenames for this generation (banner/video). */
  mediaAssetId?: string;
  /** Banner layout used for this generation. */
  bannerLayoutId?: BannerLayoutId;
  regenerationCount?: number;
  createdAt: string;
  publishedAt?: string;
  platformPermalinks?: Record<string, string>;
}

/** Persisted QA snapshot (subset of full evaluation). */
export interface GenerationQaReportSnapshot {
  scores: {
    contentQuality: number;
    humanLikeness: number;
    brandAlignment: number;
    bannerCompleteness: number;
    videoQuality: number;
    overall: number;
  };
  passed: boolean;
  threshold: number;
  layoutId: BannerLayoutId;
  layoutSeed: number;
  mediaAssetId: string;
  issues: string[];
  improvements: string[];
  attempts: number;
  captionRetried: boolean;
}

export interface ConnectedAccount {
  platform: string;
  label: string;
  /** @deprecated use profileName */
  account?: string;
  profileName: string;
  connected: boolean;
  connectionStatus: ConnectionStatus;
  accessToken?: string | null;
  refreshToken?: string | null;
  expiresAt?: string | null;
  lastSyncedAt?: string | null;
  /** Platform member/page id (e.g. LinkedIn person id for ugcPosts). */
  externalId?: string | null;
}

export interface GeneratePostRequest {
  projectId: string;
  project: Project;
  brand: BrandProfile;
  context: PostingContextData;
  sequencePostId: string;
  connectedPlatforms: string[];
  schedule?: { postsPerDay: 1 | 2; saved: boolean };
  automation?: Record<string, boolean>;
  accounts?: ConnectedAccount[];
}
