import { apiClient, PaginatedResponse, PaginationParams } from './client';

export type ProjectStatus = 'draft' | 'planning' | 'in_progress' | 'review' | 'completed' | 'archived';
export type ProjectPriority = 'low' | 'medium' | 'high' | 'urgent';
export type ContentType = 'social_post' | 'blog_article' | 'email' | 'ad_copy' | 'landing_page' | 'video_script' | 'product_description' | 'other';
export type ContentStatus = 'idea' | 'drafting' | 'review' | 'approved' | 'scheduled' | 'published' | 'archived';
export type SocialPlatform = 'facebook' | 'instagram' | 'twitter' | 'linkedin' | 'tiktok' | 'youtube' | 'pinterest';

export interface Project {
  id: string;
  tenant_id: string;
  brand_id: string;
  brand?: {
    id: string;
    name: string;
  };
  owner_id: string | null;
  owner?: {
    id: string;
    first_name: string;
    last_name: string;
    email: string;
  };
  name: string;
  slug: string;
  description: string | null;
  brief: string | null;
  status: ProjectStatus;
  priority: ProjectPriority;
  start_date: string | null;
  due_date: string | null;
  completed_at: string | null;
  tags: string[] | null;
  settings: Record<string, any> | null;
  progress: number;
  created_at: string;
  updated_at: string;
}

export interface ContentPiece {
  id: string;
  tenant_id: string;
  project_id: string;
  project?: {
    id: string;
    name: string;
  };
  assigned_to: string | null;
  assignee?: {
    id: string;
    first_name: string;
    last_name: string;
    email: string;
  };
  title: string;
  type: ContentType;
  status: ContentStatus;
  platforms: SocialPlatform[] | null;
  content: string | null;
  content_html: string | null;
  prompt_used: string | null;
  ai_metadata: Record<string, any> | null;
  asset_ids: string[] | null;
  call_to_action: string | null;
  hashtags: string[] | null;
  scheduled_at: string | null;
  published_at: string | null;
  published_url: string | null;
  version: number;
  tags: string[] | null;
  metadata: Record<string, any> | null;
  created_at: string;
  updated_at: string;
}

export const projectsApi = {
  getAll: (params?: PaginationParams & { brandId?: string; status?: ProjectStatus; search?: string }) =>
    apiClient.get<PaginatedResponse<Project>>('/projects', { params }),

  getById: (id: string) => apiClient.get<Project>(`/projects/${id}`),

  getByBrand: (brandId: string) => apiClient.get<Project[]>(`/projects/brand/${brandId}`),

  create: (data: Partial<Project>) => apiClient.post<Project>('/projects', data),

  update: (id: string, data: Partial<Project>) => apiClient.put<Project>(`/projects/${id}`, data),

  updateStatus: (id: string, status: ProjectStatus) =>
    apiClient.put<Project>(`/projects/${id}/status`, { status }),

  updateProgress: (id: string, progress: number) =>
    apiClient.put<Project>(`/projects/${id}/progress`, { progress }),

  delete: (id: string) => apiClient.delete(`/projects/${id}`),
};

export const contentPiecesApi = {
  getAll: (params?: PaginationParams & { projectId?: string; type?: ContentType; status?: ContentStatus; search?: string }) =>
    apiClient.get<PaginatedResponse<ContentPiece>>('/content-pieces', { params }),

  getById: (id: string) => apiClient.get<ContentPiece>(`/content-pieces/${id}`),

  getByProject: (projectId: string) =>
    apiClient.get<ContentPiece[]>(`/content-pieces/project/${projectId}`),

  create: (data: Partial<ContentPiece>) =>
    apiClient.post<ContentPiece>('/content-pieces', data),

  update: (id: string, data: Partial<ContentPiece>) =>
    apiClient.put<ContentPiece>(`/content-pieces/${id}`, data),

  updateStatus: (id: string, status: ContentStatus) =>
    apiClient.put<ContentPiece>(`/content-pieces/${id}/status`, { status }),

  assign: (id: string, userId: string | null) =>
    apiClient.put<ContentPiece>(`/content-pieces/${id}/assign`, { userId }),

  schedule: (id: string, scheduledAt: string) =>
    apiClient.put<ContentPiece>(`/content-pieces/${id}/schedule`, { scheduledAt }),

  publish: (id: string, publishedUrl?: string) =>
    apiClient.put<ContentPiece>(`/content-pieces/${id}/publish`, { publishedUrl }),

  duplicate: (id: string) =>
    apiClient.post<ContentPiece>(`/content-pieces/${id}/duplicate`),

  delete: (id: string) => apiClient.delete(`/content-pieces/${id}`),
};
