import { useMemo } from 'react';

import { useConvexQuery } from '@/app/hooks/use-convex-query';
import { api } from '@/convex/_generated/api';
import type { Id } from '@/convex/_generated/dataModel';
import type { ConvexItemOf } from '@/lib/types/convex-helpers';

export type ProjectListItem = ConvexItemOf<
  typeof api.projects.queries.listProjects
>;

export function useProjects(
  organizationId: string,
  options?: { includeArchived?: boolean },
) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.listProjects,
    {
      organizationId,
      includeArchived: options?.includeArchived,
    },
  );
  return {
    projects: data ?? [],
    isLoading,
  };
}

export function useProject(projectId: Id<'projects'> | undefined) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.getProject,
    projectId ? { projectId } : 'skip',
  );
  return { project: data ?? null, isLoading };
}

export function useProjectStats(projectId: Id<'projects'> | undefined) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.getProjectStats,
    projectId ? { projectId } : 'skip',
  );
  return { stats: data ?? null, isLoading };
}

export function useProjectDocuments(projectId: Id<'projects'> | undefined) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.listProjectDocuments,
    projectId ? { projectId } : 'skip',
  );
  return { documents: data ?? [], isLoading };
}

export function useProjectThreads(
  projectId: Id<'projects'> | undefined,
  scope: 'mine' | 'shared' | 'all' = 'all',
) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.listProjectThreads,
    projectId ? { projectId, scope } : 'skip',
  );
  return { threads: data ?? [], isLoading };
}

export function useSidebarProjects(organizationId: string) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.listSidebarProjects,
    { organizationId, limit: 8 },
  );
  return { projects: data ?? [], isLoading };
}

export function useProjectsSearch(organizationId: string, query: string) {
  const { data, isLoading } = useConvexQuery(
    api.projects.queries.searchProjects,
    query.trim().length > 0
      ? { organizationId, query: query.trim(), limit: 20 }
      : 'skip',
  );
  return { results: data ?? [], isLoading };
}

/**
 * Partition project threads into "yours" / "shared with project" segments
 * client-side. Uses the `scope: 'all'` query for both segments at once.
 */
export function useProjectThreadSegments(
  projectId: Id<'projects'> | undefined,
  callerUserId: string | undefined,
) {
  const { threads, isLoading } = useProjectThreads(projectId, 'all');
  return useMemo(() => {
    if (!callerUserId) return { yours: [], shared: [], isLoading };
    const yours = threads.filter((t) => t.userId === callerUserId);
    const shared = threads.filter(
      (t) => t.userId !== callerUserId && t.sharedWithProject === true,
    );
    return { yours, shared, isLoading };
  }, [threads, callerUserId, isLoading]);
}
