import { Router } from "express";
import type { Request } from "express";
import { graphqlClient } from "../lib/graphqlClient";
import { respondWithError } from "../lib/error";
import {
  GetContributionDocument,
  ContributionsDocument,
  CreateContributionDocument,
  UpdateContributionDocument,
  DeleteContributionDocument,
  ReviewContributionDocument,
} from "../../shared/generated/graphql";

interface GqlRequest extends Request {
  gql?: typeof graphqlClient;
}

const router = Router();

// GET /api/contributions/:id
router.get("/:id", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(GetContributionDocument, {
      id: req.params.id,
    })) as import("../../shared/generated/graphql").GetContributionQuery;
    if (!data.contribution)
      return res.status(404).json({ message: "Contribution not found" });
    res.json(data.contribution);
  } catch (error) {
    respondWithError(res, error, "Failed to fetch contribution");
  }
});

// GET /api/contributions
router.get("/", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(ContributionsDocument, {
      userId: req.query.userId as string | undefined,
      status: req.query.status as string | undefined,
      limit: req.query.limit ? Number(req.query.limit) : undefined,
      offset: req.query.offset ? Number(req.query.offset) : undefined,
    })) as import("../../shared/generated/graphql").ContributionsQuery;
    res.json(data.contributions);
  } catch (error) {
    respondWithError(res, error, "Failed to fetch contributions");
  }
});

// POST /api/contributions
router.post("/", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(CreateContributionDocument, {
      input: req.body,
    })) as import("../../shared/generated/graphql").CreateContributionMutation;
    res.status(201).json(data.createContribution);
  } catch (error) {
    respondWithError(res, error, "Failed to create contribution");
  }
});

// PUT /api/contributions/:id
router.put("/:id", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(UpdateContributionDocument, {
      id: req.params.id,
      input: req.body,
    })) as import("../../shared/generated/graphql").UpdateContributionMutation;
    res.json(data.updateContribution);
  } catch (error) {
    respondWithError(res, error, "Failed to update contribution");
  }
});

// DELETE /api/contributions/:id
router.delete("/:id", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(DeleteContributionDocument, {
      id: req.params.id,
    })) as import("../../shared/generated/graphql").DeleteContributionMutation;
    res.json({ success: data.deleteContribution });
  } catch (error) {
    respondWithError(res, error, "Failed to delete contribution");
  }
});

// POST /api/contributions/:id/review
router.post("/:id/review", async (req: GqlRequest, res) => {
  try {
    const client = req.gql || graphqlClient;
    const data = (await client.request(ReviewContributionDocument, {
      id: req.params.id,
      status: req.body.status,
      feedback: req.body.feedback,
    })) as import("../../shared/generated/graphql").ReviewContributionMutation;
    res.json(data.reviewContribution);
  } catch (error) {
    respondWithError(res, error, "Failed to review contribution");
  }
});

export default router;
