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

import { useQuery, useMutation } from 'urql';
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 result for single {{ model_name }} query */
export interface Use{{ model_name }}Result {
  data: {{ model_name }} | null;
  loading: boolean;
  error: Error | null;
  refetch: () => void;
}

/** Hook result for {{ model_name }} list query */
export interface Use{{ model_name }}ListResult {
  data: {{ model_name }}[];
  loading: boolean;
  error: Error | null;
  totalCount: number;
  hasNextPage: boolean;
  hasPreviousPage: boolean;
  refetch: () => void;
}

/** Hook result for mutations */
export interface Use{{ model_name }}MutationsResult {
{%- if ops_create %}
  create: (data: {{ model_name }}Create) => Promise<{{ model_name }} | null>;
{%- endif %}
{%- if ops_update %}
  update: (id: number, data: {{ model_name }}Update) => Promise<{{ model_name }} | null>;
{%- endif %}
{%- if ops_delete %}
  remove: (id: number) => Promise<boolean>;
{%- endif %}
  loading: boolean;
  error: Error | null;
}

/**
 * Hook for fetching a single {{ model_name }} by ID.
 */
export function use{{ model_name }}(id: number | null): Use{{ model_name }}Result {
  const [result, reexecute] = useQuery({
    query: Get{{ model_name }}Query,
    variables: { id: id! },
    pause: id === null,
  });

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

/**
 * Hook for fetching a list of {{ plural_name }} with pagination.
 */
export function use{{ model_name }}List(
  pagination?: PaginationParams
): Use{{ model_name }}ListResult {
  const [result, reexecute] = useQuery({
    query: List{{ model_name }}sQuery,
    variables: {
      pagination: pagination ? {
        page: pagination.page ?? 1,
        pageSize: pagination.pageSize ?? 20,
      } : undefined,
    },
  });

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

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

/**
 * Hook for {{ model_name }} mutations (create, update, delete).
 */
export function use{{ model_name }}Mutations(): Use{{ model_name }}MutationsResult {
{%- if ops_create %}
  const [createResult, executeCreate] = useMutation(Create{{ model_name }}Mutation);
{%- endif %}
{%- if ops_update %}
  const [updateResult, executeUpdate] = useMutation(Update{{ model_name }}Mutation);
{%- endif %}
{%- if ops_delete %}
  const [deleteResult, executeDelete] = useMutation(Delete{{ model_name }}Mutation);
{%- endif %}

  const loading = {{ loading_expr }};
  const error = {{ error_expr }};

  return {
{%- if ops_create %}
    create: async (data) => {
      const result = await executeCreate({ input: data });
      return result.data?.create{{ model_name }} ?? null;
    },
{%- endif %}
{%- if ops_update %}
    update: async (id, data) => {
      const result = await executeUpdate({ id, input: data });
      return result.data?.update{{ model_name }} ?? null;
    },
{%- endif %}
{%- if ops_delete %}
    remove: async (id) => {
      const result = await executeDelete({ id });
      return result.data?.delete{{ model_name }} ?? false;
    },
{%- endif %}
    loading,
    error,
  };
}

/**
 * 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,
  };
}
