import { Request, Router } from "express";
import TicketService from "../services/ticket-services";
import { sendQRToMail } from "../utils/send-QR";

const router = Router();

const getKnex = (req: Request) => req.app.get("knex");

/** Creating ticket */
router.post("/", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    const ticketId = await ticketService.create(req.body);

    sendQRToMail(ticketId);

    res.send(`Ticket was successfully created with id: ${ticketId}`);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

router.post("/multiple", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    const ticketIds = await ticketService.createMultiple(req.body);

    res.status(200).json(ticketIds);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Deleting concrete ticket */
router.delete("/:id", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    await ticketService.delete(req.params.id);
    res.send(`Ticket with ${req.params.id} was successfully deleted`);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Support delete method (deletes whole "tickets" table) */
router.delete("/delete/all", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    await ticketService.deleteAllTickets();
    res.send(`All tickets have been deleted`);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Ticket validation */
router.get("/validate/:id", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    let result = await ticketService.validate(req.params.id);

    if (result.length) {
      res.status(200).json({ valid: true });
    } else res.status(200).json({ valid: false });
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Getting all ticket types */
router.get("/type/list-all", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    let result = await ticketService.getAllTicketTypes();
    if (result.length) {
      res.status(200).json(result);
    }
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Creating ticket type */
router.post("/type", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    let result = await ticketService.createTicketType(req.body);

    res.status(200).json(result);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Deleting single ticket type */
router.delete("/type/:id", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    await ticketService.deleteTicketType(req.params.id);
    res
      .status(200)
      .send(`Ticket type ${req.params.id} was successfully deleted. `);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

/** Support delete method (deletes whole "ticket_types" table) */
router.delete("/type/delete/all", async (req, res) => {
  const knex = getKnex(req);

  try {
    const ticketService = new TicketService(knex);
    await ticketService.deleteAllTicketTypes();
    res.send(`All ticket types have been deleted`);
  } catch (err) {
    res.send(`An error has occured: ${err}`);
  }
});

export default router;
