import express, { Request, Response } from "express";
import { z } from "zod";
import { storage } from "../storage";
import { insertNpiProviderSchema } from "@repo/db/types";

const router = express.Router();

router.get("/", async (req: Request, res: Response) => {
  try {
    if (!req.user?.id) {
      return res.status(401).json({ message: "Unauthorized" });
    }

    const providers = await storage.getNpiProvidersByUser(req.user.id);
    res.status(200).json(providers);
  } catch (err) {
    res.status(500).json({
      error: "Failed to fetch NPI providers",
      details: String(err),
    });
  }
});

router.post("/", async (req: Request, res: Response) => {
  try {
    if (!req.user?.id) {
      return res.status(401).json({ message: "Unauthorized" });
    }

    const parsed = insertNpiProviderSchema.safeParse({
      ...req.body,
      userId: req.user.id,
    });

    if (!parsed.success) {
      const flat = parsed.error.flatten();
      const firstError =
        Object.values(flat.fieldErrors)[0]?.[0] || "Invalid input";

      return res.status(400).json({
        message: firstError,
        details: flat.fieldErrors,
      });
    }

    const provider = await storage.createNpiProvider(parsed.data);
    res.status(201).json(provider);
  } catch (err: any) {
    if (err.code === "P2002") {
      return res.status(400).json({
        message: "This NPI already exists for the user",
      });
    }
    res.status(500).json({
      error: "Failed to create NPI provider",
      details: String(err),
    });
  }
});

router.put("/:id", async (req: Request, res: Response) => {
  try {
    const id = Number(req.params.id);
    if (isNaN(id)) return res.status(400).send("Invalid ID");

    const provider = await storage.updateNpiProvider(id, req.body);
    res.status(200).json(provider);
  } catch (err) {
    res.status(500).json({
      error: "Failed to update NPI provider",
      details: String(err),
    });
  }
});

router.delete("/:id", async (req: Request, res: Response) => {
  try {
    if (!req.user?.id) {
      return res.status(401).json({ message: "Unauthorized" });
    }

    const id = Number(req.params.id);
    if (isNaN(id)) return res.status(400).send("Invalid ID");

    const ok = await storage.deleteNpiProvider(req.user.id, id);
    if (!ok) {
      return res.status(404).json({ message: "NPI provider not found" });
    }

    res.status(204).send();
  } catch (err) {
    res.status(500).json({
      error: "Failed to delete NPI provider",
      details: String(err),
    });
  }
});

export default router;
