const { Router } = require("express");
const prisma = require("../prisma");
const { requireAuth } = require("../middleware/auth");
const { requirePosRead, requirePosWrite } = require("../middleware/perms");
const { adjustStock, stockTransition } = require("../lib/inventory");
const { requireActiveBranch } = require("../middleware/branch");

const router = Router();

function normalizeOptionalString(input) {
  if (input === undefined) return undefined;
  if (input === null || input === "") return null;
  const s = String(input).trim();
  return s || null;
}

function prismaConflict(err, res) {
  if (err.code === "P2002") {
    return res.status(409).json({ error: "Sales return number already exists" });
  }
  return null;
}

function randomNumber() {
  const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
  let out = "SRN-";
  for (let i = 0; i < 8; i += 1) out += alphabet[Math.floor(Math.random() * alphabet.length)];
  return out;
}

function round2(n) {
  return Math.round((Number(n) + Number.EPSILON) * 100) / 100;
}

function parseLines(lines) {
  if (!Array.isArray(lines)) return [];
  return lines
    .map((l) => ({
      productId: l?.productId ? String(l.productId).trim() : "",
      quantity: Number.isFinite(Number(l?.quantity)) ? Math.floor(Number(l.quantity)) : 0,
      unitPrice: Number(l?.unitPrice) || 0,
      taxRate: Number(l?.taxRate) || 0,
    }))
    .filter((l) => l.productId && l.quantity > 0);
}

function calcTotals(lines) {
  let subtotal = 0;
  let taxTotal = 0;
  for (const l of lines) {
    const qty = Math.max(0, Math.floor(Number(l.quantity) || 0));
    const price = Number(l.unitPrice) || 0;
    const rate = Number(l.taxRate) || 0;
    const base = qty * price;
    const tax = base * (rate / 100);
    subtotal += base;
    taxTotal += tax;
  }
  const total = subtotal + taxTotal;
  return {
    subtotal: round2(subtotal),
    taxTotal: round2(taxTotal),
    total: round2(total),
  };
}

router.get("/", requireAuth, requireActiveBranch, requirePosRead, async (req, res, next) => {
  try {
    const page = Math.max(1, parseInt(req.query.page) || 1);
    const limit = Math.min(100, Math.max(1, parseInt(req.query.limit) || 25));
    const skip = (page - 1) * limit;
    const search = (req.query.search || "").trim();

    const where = { branchId: req.activeBranchId };
    if (search) {
      where.OR = [
        { number: { contains: search, mode: "insensitive" } },
      ];
    }

    const [total, rows] = await Promise.all([
      prisma.salesReturn.count({ where }),
      prisma.salesReturn.findMany({
        where,
        orderBy: [{ updatedAt: "desc" }],
        skip,
        take: limit,
        include: {
          customer: { select: { id: true, name: true } },
          warehouse: { select: { id: true, name: true } },
          sale: { select: { id: true, number: true } },
          _count: { select: { lines: true } },
        },
      }),
    ]);

    const data = rows.map((r) => ({
      id: r.id,
      number: r.number,
      status: r.status,
      returnDate: r.returnDate,
      saleId: r.saleId,
      saleNumber: r.sale?.number || null,
      customerId: r.customerId,
      customerName: r.customer?.name || null,
      warehouseId: r.warehouseId,
      warehouseName: r.warehouse?.name || null,
      lineCount: r._count?.lines || 0,
      subtotal: r.subtotal,
      taxTotal: r.taxTotal,
      total: r.total,
      createdAt: r.createdAt,
      updatedAt: r.updatedAt,
    }));

    return res.json({
      data,
      pagination: { page, limit, total, totalPages: Math.ceil(total / limit) },
    });
  } catch (err) {
    return next(err);
  }
});

router.get("/generate-number", requireAuth, requirePosRead, async (req, res, next) => {
  try {
    for (let i = 0; i < 20; i += 1) {
      const number = randomNumber();
      const exists = await prisma.salesReturn.findUnique({ where: { number } });
      if (!exists) return res.json({ number });
    }
    return res.status(503).json({ error: "Could not generate sales return number. Try again." });
  } catch (err) {
    return next(err);
  }
});

router.get("/:id", requireAuth, requireActiveBranch, requirePosRead, async (req, res, next) => {
  try {
    const row = await prisma.salesReturn.findFirst({
      where: { id: req.params.id, branchId: req.activeBranchId },
      include: {
        customer: { select: { id: true, name: true } },
        warehouse: { select: { id: true, name: true } },
        sale: { select: { id: true, number: true } },
        lines: {
          orderBy: { id: "asc" },
          include: { product: { select: { id: true, name: true, sku: true } } },
        },
      },
    });
    if (!row) return res.status(404).json({ error: "Not found" });
    return res.json(row);
  } catch (err) {
    return next(err);
  }
});

router.post("/", requireAuth, requireActiveBranch, requirePosWrite, async (req, res, next) => {
  try {
    const body = req.body || {};
    const number = String(body.number || "").trim();
    if (!number) return res.status(400).json({ error: "Missing number" });

    const status = String(body.status || "Draft");
    if (!["Draft", "Returned", "Cancelled"].includes(status)) {
      return res.status(400).json({ error: "Invalid status" });
    }

    const lines = parseLines(body.lines);
    if (lines.length === 0) return res.status(400).json({ error: "Add at least one line item" });

    const productIds = lines.map((l) => l.productId);
    const products = await prisma.product.findMany({
      where: { id: { in: productIds } },
      select: { id: true, name: true, isReturnable: true },
    });
    const nonReturnable = products.filter((p) => p.isReturnable === false);
    if (nonReturnable.length > 0) {
      return res.status(400).json({
        error: `Non-returnable product(s): ${nonReturnable.map((p) => p.name).join(", ")}`,
      });
    }

    const totals = calcTotals(lines);
    const returnDate = body.returnDate ? new Date(body.returnDate) : new Date();
    const warehouseId = normalizeOptionalString(body.warehouseId);

    const row = await prisma.$transaction(async (tx) => {
      const created = await tx.salesReturn.create({
        data: {
          number,
          saleId: normalizeOptionalString(body.saleId),
          customerId: normalizeOptionalString(body.customerId),
          warehouseId,
          branchId: req.activeBranchId,
          status,
          returnDate: Number.isNaN(returnDate.getTime()) ? new Date() : returnDate,
          reason: normalizeOptionalString(body.reason),
          notes: normalizeOptionalString(body.notes),
          ...totals,
          lines: {
            create: lines.map((l) => ({
              productId: l.productId,
              quantity: l.quantity,
              unitPrice: l.unitPrice,
              taxRate: l.taxRate,
              lineTotal: round2(l.quantity * l.unitPrice * (1 + l.taxRate / 100)),
            })),
          },
        },
        include: {
          customer: { select: { id: true, name: true } },
          warehouse: { select: { id: true, name: true } },
          sale: { select: { id: true, number: true } },
          lines: { include: { product: { select: { id: true, name: true, sku: true } } } },
        },
      });

      if (status === "Returned") {
        await adjustStock(tx, lines, 1, warehouseId);
      }
      return created;
    });
    return res.status(201).json(row);
  } catch (err) {
    const r = prismaConflict(err, res);
    if (r) return r;
    return next(err);
  }
});

router.patch("/:id", requireAuth, requireActiveBranch, requirePosWrite, async (req, res, next) => {
  try {
    const { id } = req.params;
    const existing = await prisma.salesReturn.findFirst({ where: { id, branchId: req.activeBranchId } });
    if (!existing) return res.status(404).json({ error: "Not found" });

    const body = req.body || {};
    const data = {};

    if (body.number !== undefined) {
      const number = String(body.number || "").trim();
      if (!number) return res.status(400).json({ error: "Number cannot be empty" });
      data.number = number;
    }
    if (body.saleId !== undefined) data.saleId = normalizeOptionalString(body.saleId);
    if (body.customerId !== undefined) data.customerId = normalizeOptionalString(body.customerId);
    if (body.warehouseId !== undefined) data.warehouseId = normalizeOptionalString(body.warehouseId);
    if (body.status !== undefined) {
      const status = String(body.status || "");
      if (!["Draft", "Returned", "Cancelled"].includes(status)) return res.status(400).json({ error: "Invalid status" });
      data.status = status;
    }
    if (body.returnDate !== undefined) {
      const d = body.returnDate ? new Date(body.returnDate) : null;
      data.returnDate = d && !Number.isNaN(d.getTime()) ? d : existing.returnDate;
    }
    if (body.reason !== undefined) data.reason = normalizeOptionalString(body.reason);
    if (body.notes !== undefined) data.notes = normalizeOptionalString(body.notes);

    const lines = body.lines !== undefined ? parseLines(body.lines) : undefined;
    const newStatus = data.status || existing.status;
    const { apply, reverse } = stockTransition(existing.status, newStatus, "Returned");

    const row = await prisma.$transaction(async (tx) => {
      if (reverse) {
        const oldLines = await tx.salesReturnLine.findMany({ where: { salesReturnId: id } });
        await adjustStock(tx, oldLines, -1, existing.warehouseId);
      }

      if (lines) {
        if (lines.length === 0) throw Object.assign(new Error("Add at least one line item"), { status: 400 });
        await tx.salesReturnLine.deleteMany({ where: { salesReturnId: id } });
      }

      const totals = lines ? calcTotals(lines) : null;
      const updated = await tx.salesReturn.update({
        where: { id },
        data: {
          ...data,
          ...(totals ? totals : {}),
          ...(lines
            ? {
                lines: {
                  create: lines.map((l) => ({
                    productId: l.productId,
                    quantity: l.quantity,
                    unitPrice: l.unitPrice,
                    taxRate: l.taxRate,
                    lineTotal: round2(l.quantity * l.unitPrice * (1 + l.taxRate / 100)),
                  })),
                },
              }
            : {}),
        },
        include: {
          customer: { select: { id: true, name: true } },
          warehouse: { select: { id: true, name: true } },
          sale: { select: { id: true, number: true } },
          lines: { include: { product: { select: { id: true, name: true, sku: true } } } },
        },
      });

      if (apply) {
        await adjustStock(tx, updated.lines, 1, updated.warehouseId);
      }
      return updated;
    });

    return res.json(row);
  } catch (err) {
    if (err?.status === 400) return res.status(400).json({ error: err.message });
    const r = prismaConflict(err, res);
    if (r) return r;
    return next(err);
  }
});

router.delete("/:id", requireAuth, requireActiveBranch, requirePosWrite, async (req, res, next) => {
  try {
    const { id } = req.params;
    const row = await prisma.salesReturn.findFirst({
      where: { id, branchId: req.activeBranchId },
      include: { lines: true },
    });
    if (!row) return res.status(404).json({ error: "Not found" });

    await prisma.$transaction(async (tx) => {
      if (row.status === "Returned") {
        await adjustStock(tx, row.lines, -1, row.warehouseId);
      }
      await tx.salesReturn.delete({ where: { id } });
    });
    return res.json({ ok: true });
  } catch (err) {
    return next(err);
  }
});

module.exports = router;

