import { CreateClientSchema, UpdateClientSchema } from "@capakraken/shared";
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { createAuditEntry } from "../lib/audit.js";
import {
  adminProcedure,
  createTRPCRouter,
  managerProcedure,
  planningReadProcedure,
  protectedProcedure,
} from "../trpc.js";

import type { ClientTree } from "@capakraken/shared";

interface FlatClient {
  id: string;
  name: string;
  code: string | null;
  parentId: string | null;
  isActive: boolean;
  sortOrder: number;
  tags: string[];
  createdAt: Date;
  updatedAt: Date;
}

function buildClientTree(flatItems: FlatClient[], parentId: string | null = null): ClientTree[] {
  return flatItems
    .filter((item) => item.parentId === parentId)
    .sort((a, b) => a.sortOrder - b.sortOrder || a.name.localeCompare(b.name))
    .map((item) => ({
      ...item,
      children: buildClientTree(flatItems, item.id),
    }));
}

export const clientRouter = createTRPCRouter({
  list: planningReadProcedure
    .input(
      z.object({
        parentId: z.string().nullable().optional(),
        isActive: z.boolean().optional(),
        search: z.string().optional(),
      }).optional(),
    )
    .query(async ({ ctx, input }) => {
      return ctx.db.client.findMany({
        where: {
          ...(input?.parentId !== undefined ? { parentId: input.parentId } : {}),
          ...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
          ...(input?.search
            ? {
                OR: [
                  { name: { contains: input.search, mode: "insensitive" as const } },
                  { code: { contains: input.search, mode: "insensitive" as const } },
                ],
              }
            : {}),
        },
        include: { _count: { select: { children: true, projects: true } } },
        orderBy: [{ sortOrder: "asc" }, { name: "asc" }],
      });
    }),

  getTree: planningReadProcedure
    .input(z.object({ isActive: z.boolean().optional() }).optional())
    .query(async ({ ctx, input }) => {
      const all = await ctx.db.client.findMany({
        where: {
          ...(input?.isActive !== undefined ? { isActive: input.isActive } : {}),
        },
        orderBy: [{ sortOrder: "asc" }, { name: "asc" }],
      });
      return buildClientTree(all);
    }),

  getById: planningReadProcedure
    .input(z.object({ id: z.string() }))
    .query(async ({ ctx, input }) => {
      const client = await findUniqueOrThrow(
        ctx.db.client.findUnique({
          where: { id: input.id },
          include: {
            parent: true,
            children: { orderBy: { sortOrder: "asc" } },
            _count: { select: { projects: true, children: true } },
          },
        }),
        "Client",
      );
      return client;
    }),

  resolveByIdentifier: protectedProcedure
    .input(z.object({ identifier: z.string().trim().min(1) }))
    .query(async ({ ctx, input }) => {
      const identifier = input.identifier.trim();
      const select = {
        id: true,
        name: true,
        code: true,
        parentId: true,
        isActive: true,
      } as const;

      let client = await ctx.db.client.findUnique({
        where: { id: identifier },
        select,
      });

      if (!client) {
        client = await ctx.db.client.findUnique({
          where: { code: identifier },
          select,
        });
      }

      if (!client) {
        client = await ctx.db.client.findFirst({
          where: { name: { equals: identifier, mode: "insensitive" } },
          select,
        });
      }

      if (!client) {
        client = await ctx.db.client.findFirst({
          where: {
            OR: [
              { name: { contains: identifier, mode: "insensitive" } },
              { code: { contains: identifier, mode: "insensitive" } },
            ],
          },
          select,
        });
      }

      if (!client) {
        throw new TRPCError({ code: "NOT_FOUND", message: `Client not found: ${identifier}` });
      }

      return client;
    }),

  getByIdentifier: planningReadProcedure
    .input(z.object({ identifier: z.string().trim().min(1) }))
    .query(async ({ ctx, input }) => {
      const identifier = input.identifier.trim();
      let client = await ctx.db.client.findUnique({
        where: { id: identifier },
        include: { _count: { select: { projects: true, children: true } } },
      });

      if (!client) {
        client = await ctx.db.client.findUnique({
          where: { code: identifier },
          include: { _count: { select: { projects: true, children: true } } },
        });
      }

      if (!client) {
        client = await ctx.db.client.findFirst({
          where: { name: { equals: identifier, mode: "insensitive" } },
          include: { _count: { select: { projects: true, children: true } } },
        });
      }

      if (!client) {
        client = await ctx.db.client.findFirst({
          where: {
            OR: [
              { name: { contains: identifier, mode: "insensitive" } },
              { code: { contains: identifier, mode: "insensitive" } },
            ],
          },
          include: { _count: { select: { projects: true, children: true } } },
        });
      }

      if (!client) {
        throw new TRPCError({ code: "NOT_FOUND", message: `Client not found: ${identifier}` });
      }

      return client;
    }),

  create: managerProcedure
    .input(CreateClientSchema)
    .mutation(async ({ ctx, input }) => {
      if (input.parentId) {
        await findUniqueOrThrow(
          ctx.db.client.findUnique({ where: { id: input.parentId } }),
          "Parent client",
        );
      }

      if (input.code) {
        const codeConflict = await ctx.db.client.findUnique({ where: { code: input.code } });
        if (codeConflict) {
          throw new TRPCError({ code: "CONFLICT", message: `Client code "${input.code}" already exists` });
        }
      }

      const created = await ctx.db.client.create({
        data: {
          name: input.name,
          ...(input.code ? { code: input.code } : {}),
          ...(input.parentId ? { parentId: input.parentId } : {}),
          sortOrder: input.sortOrder,
          ...(input.tags ? { tags: input.tags } : {}),
        },
      });

      void createAuditEntry({
        db: ctx.db,
        entityType: "Client",
        entityId: created.id,
        entityName: created.name,
        action: "CREATE",
        userId: ctx.dbUser?.id,
        after: created as unknown as Record<string, unknown>,
        source: "ui",
      });

      return created;
    }),

  update: managerProcedure
    .input(z.object({ id: z.string(), data: UpdateClientSchema }))
    .mutation(async ({ ctx, input }) => {
      const existing = await findUniqueOrThrow(
        ctx.db.client.findUnique({ where: { id: input.id } }),
        "Client",
      );

      if (input.data.code && input.data.code !== existing.code) {
        const conflict = await ctx.db.client.findUnique({ where: { code: input.data.code } });
        if (conflict) {
          throw new TRPCError({ code: "CONFLICT", message: `Client code "${input.data.code}" already exists` });
        }
      }

      const before = existing as unknown as Record<string, unknown>;

      const updated = await ctx.db.client.update({
        where: { id: input.id },
        data: {
          ...(input.data.name !== undefined ? { name: input.data.name } : {}),
          ...(input.data.code !== undefined ? { code: input.data.code } : {}),
          ...(input.data.sortOrder !== undefined ? { sortOrder: input.data.sortOrder } : {}),
          ...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
          ...(input.data.parentId !== undefined ? { parentId: input.data.parentId } : {}),
          ...(input.data.tags !== undefined ? { tags: input.data.tags } : {}),
        },
      });

      void createAuditEntry({
        db: ctx.db,
        entityType: "Client",
        entityId: updated.id,
        entityName: updated.name,
        action: "UPDATE",
        userId: ctx.dbUser?.id,
        before,
        after: updated as unknown as Record<string, unknown>,
        source: "ui",
      });

      return updated;
    }),

  deactivate: managerProcedure
    .input(z.object({ id: z.string() }))
    .mutation(async ({ ctx, input }) => {
      const updated = await ctx.db.client.update({
        where: { id: input.id },
        data: { isActive: false },
      });

      void createAuditEntry({
        db: ctx.db,
        entityType: "Client",
        entityId: updated.id,
        entityName: updated.name,
        action: "UPDATE",
        userId: ctx.dbUser?.id,
        before: { isActive: true },
        after: { isActive: false },
        source: "ui",
        summary: "Deactivated Client",
      });

      return updated;
    }),

  delete: adminProcedure
    .input(z.object({ id: z.string() }))
    .mutation(async ({ ctx, input }) => {
      const client = await findUniqueOrThrow(
        ctx.db.client.findUnique({
          where: { id: input.id },
          include: { _count: { select: { projects: true, children: true } } },
        }),
        "Client",
      );
      if (client._count.projects > 0) {
        throw new TRPCError({
          code: "PRECONDITION_FAILED",
          message: `Cannot delete client with ${client._count.projects} project(s). Deactivate instead.`,
        });
      }
      if (client._count.children > 0) {
        throw new TRPCError({
          code: "PRECONDITION_FAILED",
          message: `Cannot delete client with ${client._count.children} child client(s). Remove children first.`,
        });
      }
      await ctx.db.client.delete({ where: { id: input.id } });

      void createAuditEntry({
        db: ctx.db,
        entityType: "Client",
        entityId: client.id,
        entityName: client.name,
        action: "DELETE",
        userId: ctx.dbUser?.id,
        before: client as unknown as Record<string, unknown>,
        source: "ui",
      });

      return client;
    }),

  batchUpdateSortOrder: managerProcedure
    .input(z.array(z.object({ id: z.string(), sortOrder: z.number().int() })))
    .mutation(async ({ ctx, input }) => {
      await ctx.db.$transaction(
        input.map((item) =>
          ctx.db.client.update({
            where: { id: item.id },
            data: { sortOrder: item.sortOrder },
          }),
        ),
      );

      for (const item of input) {
        void createAuditEntry({
          db: ctx.db,
          entityType: "Client",
          entityId: item.id,
          action: "UPDATE",
          userId: ctx.dbUser?.id,
          after: { sortOrder: item.sortOrder },
          source: "ui",
          summary: "Updated sort order",
        });
      }

      return { ok: true };
    }),
});
