import { isApiSuccess } from "@/lib/api/errors";
import type { OpportunityFeedbackRecord } from "@/features/opportunities/permissionRecord";
import apiClient from "@/lib/api/client";
import type { ApiResponse } from "@/lib/api/types";

/**
 * `OpportunityFeedbackController::store()`.
 *
 * `rating` is `['required', 'integer', 'min:1', 'max:5']` — the bound is the
 * server's, and the type says what the server accepts rather than restating
 * the slider's range. The reviewed business rule requires a nonblank comment; BE-217
 * tightens the legacy nullable server validation.
 *
 * `primary_language` is `Rule::in(['en', 'ar'])`, so a bare string would be a
 * 422; the union is the whole accepted set.
 *
 * No `user_id` — the controller merges `$request->user()->id` itself, and
 * sending one would be ignored.
 */
export interface CreateOpportunityFeedbackBody {
  learn_serve_opportunity_id: number;
  /** 1–5 inclusive. */
  rating: number;
  comment_en?: string | null;
  comment_ar?: string | null;
  primary_language?: "en" | "ar";
}

/**
 * `update()` revalidates everything but the opportunity id, which it does not
 * accept at all — a review cannot be moved to another opportunity. `rating` is
 * `sometimes` here rather than `required`.
 */
export type UpdateOpportunityFeedbackBody = Partial<
  Omit<CreateOpportunityFeedbackBody, "learn_serve_opportunity_id">
>;

/* The created review, in the same shape the list returns. 201. */
export const createOpportunityFeedback = (
  data: CreateOpportunityFeedbackBody
): Promise<ApiResponse<OpportunityFeedbackRecord>> =>
  apiClient.post("/opportunity-feedbacks/", data).then((r) => { if (!isApiSuccess(r.data)) throw r.data; return r.data; });

export const getOpportunityFeedbacks = (
  opportunity_id: string
): Promise<ApiResponse<OpportunityFeedbackRecord[]>> =>
  apiClient.get("/opportunity-feedbacks/", { params: { opportunity_id } }).then((r) => { if (!isApiSuccess(r.data)) throw r.data; return r.data; });

/**
 * A toggle — and, unlike the community `/likes/toggle/`, it reports **the like
 * row**, not a recounted total. There is no `likes_count` here, so a caller
 * that wants the new total has to refetch the review rather than read it off
 * this reply.
 *
 * No caller at present: the reviews list reads `is_liked_by_me` and
 * `likes_count` off the feedback record and has no like control yet. Typed
 * because the route exists; if it stays unused it should be deleted rather
 * than left untyped.
 */
export interface OpportunityFeedbackLikeRecord {
  id: number;
  feedback_id: number;
  user_id: number;
  is_liked: boolean;
}

export const likeOpportunityFeedback = (
  feedback_id: string
): Promise<ApiResponse<OpportunityFeedbackLikeRecord>> =>
  apiClient.post(`/opportunity-feedback/${feedback_id}/like/`).then((r) => r.data);

export const updateOpportunityFeedback = ({
  feedback_id,
  data,
}: {
  feedback_id: string;
  data: UpdateOpportunityFeedbackBody;
}): Promise<ApiResponse<OpportunityFeedbackRecord>> =>
  apiClient.patch(`/opportunity-feedbacks/${feedback_id}/`, data).then((r) => { if (!isApiSuccess(r.data)) throw r.data; return r.data; });

export const deleteOpportunityFeedback = (feedback_id: string): Promise<ApiResponse<null>> =>
  apiClient.delete(`/opportunity-feedbacks/${feedback_id}/`).then((r) => { if (!isApiSuccess(r.data)) throw r.data; return r.data; });
