/**
 * React hooks for {{ model_name }} using Apollo Client.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

import { useQuery, useMutation } from '@apollo/client';
import type { {{ model_name }}, {{ model_name }}Create, {{ model_name }}Update, PaginationParams } from '../types/generated';

// Import generated GraphQL operations
import {
  Get{{ model_name }}Query,
  List{{ model_name }}sQuery,
{%- if ops_create %}
  Create{{ model_name }}Mutation,
{%- endif %}
{%- if ops_update %}
  Update{{ model_name }}Mutation,
{%- endif %}
{%- if ops_delete %}
  Delete{{ model_name }}Mutation,
{%- endif %}
} from '../graphql';

/**
 * Hook for fetching a single {{ model_name }} by ID.
 */
export function use{{ model_name }}(id: number | null) {
  const { data, loading, error, refetch } = useQuery(Get{{ model_name }}Query, {
    variables: { id: id! },
    skip: id === null,
  });

  return {
    data: data?.{{ camel_name }} ?? null,
    loading,
    error: error ?? null,
    refetch,
  };
}

/**
 * Hook for fetching a list of {{ plural_name }} with pagination.
 */
export function use{{ model_name }}List(pagination?: PaginationParams) {
  const { data, loading, error, refetch } = useQuery(List{{ model_name }}sQuery, {
    variables: {
      pagination: pagination ? {
        page: pagination.page ?? 1,
        pageSize: pagination.pageSize ?? 20,
      } : undefined,
    },
  });

  const connection = data?.{{ plural_camel }};

  return {
    data: connection?.edges.map((e: { node: {{ model_name }} }) => e.node) ?? [],
    loading,
    error: error ?? null,
    totalCount: connection?.totalCount ?? 0,
    hasNextPage: connection?.pageInfo.hasNextPage ?? false,
    hasPreviousPage: connection?.pageInfo.hasPreviousPage ?? false,
    refetch,
  };
}

/**
 * Hook for {{ model_name }} mutations.
 */
export function use{{ model_name }}Mutations() {
{%- if ops_create %}
  const [createMutation, createState] = useMutation(Create{{ model_name }}Mutation);
{%- endif %}
{%- if ops_update %}
  const [updateMutation, updateState] = useMutation(Update{{ model_name }}Mutation);
{%- endif %}
{%- if ops_delete %}
  const [deleteMutation, deleteState] = useMutation(Delete{{ model_name }}Mutation);
{%- endif %}

  return {
{%- if ops_create %}
    create: async (data: {{ model_name }}Create) => {
      const result = await createMutation({ variables: { input: data } });
      return result.data?.create{{ model_name }} ?? null;
    },
{%- endif %}
{%- if ops_update %}
    update: async (id: number, data: {{ model_name }}Update) => {
      const result = await updateMutation({ variables: { id, input: data } });
      return result.data?.update{{ model_name }} ?? null;
    },
{%- endif %}
{%- if ops_delete %}
    remove: async (id: number) => {
      const result = await deleteMutation({ variables: { id } });
      return result.data?.delete{{ model_name }} ?? false;
    },
{%- endif %}
    loading: {{ loading_expr }},
    error: {{ error_expr }},
  };
}

/**
 * Combined hook for all {{ model_name }} operations.
 */
export function use{{ model_name }}s(pagination?: PaginationParams) {
  const list = use{{ model_name }}List(pagination);
  const mutations = use{{ model_name }}Mutations();

  return {
    ...list,
    ...mutations,
  };
}
