/**
 * Team API service — CRUD for teams, members, drafts, main memos,
 * comments, review flow, reactions, and user profile resolution.
 */

import { fetchJson } from "./apiClient";

// ====================== TYPES ======================

export interface UserProfile {
    user_id: string;
    display_name: string;
    avatar_url: string;
    email: string;
}

export interface TeamMember {
    user_id: string;
    role: "owner" | "member";
    joined_at?: string;
    display_name: string;
    avatar_url: string;
}

export interface Team {
    id: string;
    name: string;
    description: string;
    owner_id: string;
    invite_code: string;
    members: TeamMember[];
    member_count: number;
    created_at?: string;
}

export type MemoStatus = "draft" | "pending_review" | "approved" | "merged";

export interface TeamMemo {
    id: string;
    content: string;
    tags: string[];
    creator_id: string;
    creator_name: string;
    creator_avatar: string;
    team_id: string;
    space: "draft" | "main";
    status: MemoStatus;
    pinned: boolean;
    merged_at?: string;
    merged_by?: string;
    merged_by_name: string;
    comment_count: number;
    reaction_counts: Record<string, number>;
    user_reactions: string[];
    review_reason?: string;
    created_at?: string;
    updated_at?: string;
}

export interface TeamComment {
    id: string;
    memo_id: string;
    user_id: string;
    user_name: string;
    user_avatar: string;
    content: string;
    created_at?: string;
}

// ====================== TEAM CRUD ======================

export const createTeam = (name: string, description = "") =>
    fetchJson<Team>("/teams", { method: "POST", body: { name, description } });

export const listTeams = () => fetchJson<Team[]>("/teams");

export const getTeam = (teamId: string) => fetchJson<Team>(`/teams/${teamId}`);

export const updateTeam = (teamId: string, data: { name?: string; description?: string }) =>
    fetchJson<Team>(`/teams/${teamId}`, { method: "PATCH", body: data });

export const deleteTeam = (teamId: string) =>
    fetchJson<{ status: string }>(`/teams/${teamId}`, { method: "DELETE" });

// ====================== MEMBERS ======================

export const joinTeam = (inviteCode: string) =>
    fetchJson<Team>("/teams/join", { method: "POST", body: { invite_code: inviteCode } });

export const listMembers = (teamId: string) =>
    fetchJson<{ members: TeamMember[] }>(`/teams/${teamId}/members`);

export const removeMember = (teamId: string, userId: string) =>
    fetchJson<{ status: string }>(`/teams/${teamId}/members/${userId}`, { method: "DELETE" });

export const leaveTeam = (teamId: string) =>
    fetchJson<{ status: string }>(`/teams/${teamId}/leave`, { method: "POST" });

// ====================== USER PROFILES ======================

export const resolveUsers = (userIds: string[]) =>
    fetchJson<{ profiles: Record<string, UserProfile> }>("/teams/resolve-users", {
        method: "POST",
        body: { user_ids: userIds },
    });

// ====================== TEAM MEMOS ======================

export const createDraft = (teamId: string, content: string, tags: string[] = []) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos`, { method: "POST", body: { content, tags } });

export const listDrafts = (teamId: string) => fetchJson<TeamMemo[]>(`/teams/${teamId}/drafts`);

export const listMain = (teamId: string) => fetchJson<TeamMemo[]>(`/teams/${teamId}/main`);

export const mergeMemo = (teamId: string, memoId: string) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}/merge`, { method: "POST" });

export const unmergeMemo = (teamId: string, memoId: string) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}/unmerge`, { method: "POST" });

export const pinMemo = (teamId: string, memoId: string) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}/pin`, { method: "POST" });

export const updateTeamMemo = (teamId: string, memoId: string, content: string, tags: string[] = []) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}`, { method: "PATCH", body: { content, tags } });

export const deleteTeamMemo = (teamId: string, memoId: string) =>
    fetchJson<{ status: string }>(`/teams/${teamId}/memos/${memoId}`, { method: "DELETE" });

// ====================== REVIEW FLOW ======================

export const requestReview = (teamId: string, memoId: string) =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}/request-review`, { method: "POST" });

export const submitReview = (teamId: string, memoId: string, action: "approve" | "reject", reason = "") =>
    fetchJson<TeamMemo>(`/teams/${teamId}/memos/${memoId}/review`, {
        method: "POST",
        body: { action, reason },
    });

// ====================== COMMENTS ======================

export const listComments = (teamId: string, memoId: string) =>
    fetchJson<TeamComment[]>(`/teams/${teamId}/memos/${memoId}/comments`);

export const createComment = (teamId: string, memoId: string, content: string) =>
    fetchJson<TeamComment>(`/teams/${teamId}/memos/${memoId}/comments`, {
        method: "POST",
        body: { content },
    });

export const deleteComment = (teamId: string, commentId: string) =>
    fetchJson<{ status: string }>(`/teams/${teamId}/comments/${commentId}`, { method: "DELETE" });

// ====================== REACTIONS ======================

export const toggleReaction = (teamId: string, memoId: string, emoji: string) =>
    fetchJson<{ memo_id: string; reaction_counts: Record<string, number>; user_reactions: string[] }>(
        `/teams/${teamId}/memos/${memoId}/reactions`,
        { method: "POST", body: { emoji } },
    );
