// GET  /api/v1/customer/accounts/:id — customer detail
// PATCH /api/v1/customer/accounts/:id — update customer

import { NextResponse } from "next/server";
import { prisma } from "@dpf/db";
import { updateCustomerSchema } from "@dpf/validators";
import { authenticateRequest } from "@/lib/api/auth-middleware";
import { ApiError, apiError } from "@/lib/api/error";
import { apiSuccess } from "@/lib/api/response";

export async function GET(
  request: Request,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    await authenticateRequest(request);
    const { id } = await params;

    const account = await prisma.customerAccount.findUnique({
      where: { id },
      include: {
        contacts: true,
        contactRoles: {
          include: { contact: true },
          orderBy: [{ isPrimary: "desc" }, { startedAt: "desc" }],
        },
        customerSites: {
          include: {
            primaryAddress: {
              include: {
                city: {
                  include: {
                    region: {
                      include: {
                        country: true,
                      },
                    },
                  },
                },
              },
            },
            nodes: {
              orderBy: [{ createdAt: "asc" }],
            },
          },
          orderBy: [{ name: "asc" }],
        },
        configurationItems: {
          orderBy: [{ updatedAt: "desc" }],
          select: {
            id: true,
            customerCiId: true,
            name: true,
            ciType: true,
            technologySourceType: true,
            lifecycleStatus: true,
            supportStatus: true,
            recommendedAction: true,
            billingCadence: true,
            customerChargeModel: true,
            site: { select: { id: true, name: true } },
          },
        },
        parentAccount: { select: { id: true, accountId: true, name: true } },
        childAccounts: { select: { id: true, accountId: true, name: true, status: true } },
      },
    });

    if (!account) {
      throw apiError("NOT_FOUND", "Customer account not found", 404);
    }

    return apiSuccess(account);
  } catch (e) {
    if (e instanceof ApiError) return e.toResponse();
    return NextResponse.json(
      { code: "INTERNAL_ERROR", message: "An unexpected error occurred" },
      { status: 500 },
    );
  }
}

export async function PATCH(
  request: Request,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    await authenticateRequest(request);
    const { id } = await params;

    const body = await request.json();
    const parsed = updateCustomerSchema.safeParse(body);
    if (!parsed.success) {
      return NextResponse.json(
        {
          code: "VALIDATION_ERROR",
          message: "Invalid input",
          details: parsed.error.flatten(),
        },
        { status: 422 },
      );
    }

    const existing = await prisma.customerAccount.findUnique({
      where: { id },
      select: { id: true },
    });
    if (!existing) {
      throw apiError("NOT_FOUND", "Customer account not found", 404);
    }

    const { name, ...rest } = parsed.data;
    const defined = Object.fromEntries(
      Object.entries(rest).filter(([, v]) => v !== undefined),
    );

    const updated = await prisma.customerAccount.update({
      where: { id },
      data: {
        ...(name !== undefined ? { name: name.trim() } : {}),
        ...defined,
      },
      include: { contacts: true },
    });

    return apiSuccess(updated);
  } catch (e) {
    if (e instanceof ApiError) return e.toResponse();
    return NextResponse.json(
      { code: "INTERNAL_ERROR", message: "An unexpected error occurred" },
      { status: 500 },
    );
  }
}
