text: const router = require("express").Router();
  const { pool } = require("../config");
  const verifyToken = require("../middleware/verify-token");
  
  router.get("/", async (req, res) => {
    try {
      const results = await pool.query(`SELECT 
      "gonderi"."gonderi_id",
      "gonderi"."gonderi_tipi",
      "gonderi"."baslik",
      "spor"."spor_adi",
      "gonderi"."icerik",
      "gonderi"."gonderiTarihi",
      "kullanici"."kullanici_adi"
       FROM "gonderi"
        INNER JOIN "kullanici" ON "gonderi"."kullanici_id" = "kullanici"."kullanici_id"
        INNER JOIN "spor" ON "gonderi"."kategori" = "spor"."spor_id"
        WHERE "gonderi_tipi" = 'S';`);
  
      const gonderiler = results.rows;
  
      res.status(200).json(gonderiler);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.get("/tags", async (req, res) => {
    try {
      const results = await pool.query(`SELECT 
      "gonderi".gonderi_id ,
      "etiket"."etiket_adi"
      FROM "gonderi"
      left OUTER JOIN "gonderietiket" ON "gonderi"."gonderi_id" = "gonderietiket"."gonderi"
      LEFT OUTER JOIN "etiket" ON "gonderietiket"."etiket" = "etiket"."etiket_id"
      WHERE "etiket"."etiket_adi" IS NOT NULL;`);
  
      res.status(200).json(results.rows);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.get("/allTags", async (req, res) => {
    try {
      const results = await pool.query(`SELECT * from "etiket";`);
  
      res.status(200).json(results.rows);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.get("/:id", async (req, res) => {
    try {
      const { id } = req.params;
      const results = await pool.query(
        `
      SELECT 
      "gonderi"."gonderi_id",
      "gonderi"."gonderi_tipi",
      "gonderi"."baslik",
      "spor"."spor_adi",
      "gonderi"."icerik",
      "gonderi"."gonderiTarihi",
      "kullanici"."kullanici_adi"
     FROM "gonderi"
      INNER JOIN "kullanici" ON "gonderi"."kullanici_id" = "kullanici"."kullanici_id"
      INNER JOIN "spor" ON "gonderi"."kategori" = "spor"."spor_id" WHERE "gonderi_id"='${id}';`
      );
      const gonderi = results.rows;
  
      res.status(200).json(gonderi);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.post("/", async (req, res) => {
    try {
      const { post } = req.body;
      const users = await pool.query(
        `SELECT kullanici_id FROM kullanici where kullanici_adi = '${post.username}' `
      );
  
      const user = parseInt(users.rows[0].kullanici_id);
  
      const results = await pool.query(
        "INSERT INTO gonderi(icerik, baslik,kategori,gonderi_tipi,kullanici_id) values($1, $2,$3,$4,$5) RETURNING *",
        [post.content, post.title, post.sport, "S", user]
      );
  
      const questions = await pool.query(
        "INSERT INTO soru(gonderi_id) values($1)",
        [results.rows[0].gonderi_id]
      );
      // Maybe it can be trriger
      for (let i = 0; i < post.tags.length; i++) {
        const tag = post.tags[i];
        const addTagToPost = await pool.query(
          `INSERT INTO gonderietiket(etiket, gonderi) values($1, $2)`,
          [tag.etiket_id, results.rows[0].gonderi_id]
        );
      }
      res.status(200).json(results.rows);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.post("/answer", async (req, res) => {
    try {
      const { post, gonderi_id } = req.body;
      console.log(gonderi_id);
      const users = await pool.query(
        `SELECT kullanici_id FROM kullanici where kullanici_adi = '${post.username}' `
      );
  
      const user = parseInt(users.rows[0].kullanici_id);
  
      const results = await pool.query(
        "INSERT INTO gonderi(icerik, baslik,kategori,gonderi_tipi,kullanici_id) values($1, $2,$3,$4,$5) RETURNING *",
        [post.content, post.title, 0, "C", user]
      );
      console.log(results.rows[0]);
      const answers = await pool.query(
        "INSERT INTO cevap(gonderi_id,soru) values($1,$2)",
        [results.rows[0].gonderi_id, gonderi_id]
      );
  
      res.status(200).json(results.rows);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.post("/vote", async (req, res) => {
    try {
      const { user, post, voteType } = req.body;
      const usersId = await pool.query(
        `SELECT "kullanici"."kullanici_id" FROM "kullanici" WHERE "kullanici_adi"='${user}';`
      );
  
      const votes = await pool.query(`SELECT * FROM "oy";`);
      const allVote = votes.rows;
  
      const userId = usersId.rows[0].kullanici_id;
  
      for (let i = 0; i < allVote.length; i++) {
        const voteUser = allVote[i].oy_atan_kullanici;
        const votePost = allVote[i].oy_atilan_gonderi;
  
        if (votePost === post && voteUser === userId) {
          return res
            .status(200)
            .json({ success: false, message: "The vote exist" });
        }
      }
  
      await pool.query(
        `INSERT INTO oy(oy_atan_kullanici, oy_atilan_gonderi,oy_tipi) values($1, $2, $3)`,
        [userId, post, voteType]
      );
  
      const positiveVoteCount = await pool.query(
        `SELECT COUNT(*) FROM "oy" WHERE "oy_tipi"=TRUE AND "oy_atilan_gonderi"=${post};`
      );
      const negativeVoteCount = await pool.query(
        `SELECT COUNT(*) FROM "oy" WHERE "oy_tipi"=FALSE AND "oy_atilan_gonderi"=${post};`
      );
  
      res.status(200).json({
        voteUp: positiveVoteCount.rows[0].count,
        voteDown: negativeVoteCount.rows[0].count
      });
    } catch (error) {
      console.log(error);
    }
  });
  
  router.get("/vote/:id", async (req, res) => {
    try {
      const { id } = req.params;
      const positiveVoteCount = await pool.query(
        `SELECT COUNT(*) FROM "oy" WHERE "oy_tipi"=TRUE AND "oy_atilan_gonderi"=${id};`
      );
      const negativeVoteCount = await pool.query(
        `SELECT COUNT(*) FROM "oy" WHERE "oy_tipi"=FALSE AND "oy_atilan_gonderi"=${id};`
      );
  
      res.status(200).json({
        voteUp: positiveVoteCount.rows[0].count,
        voteDown: negativeVoteCount.rows[0].count
      });
    } catch (error) {
      console.log(error);
    }
  });
  
  router.get("/answer/:id", async (req, res) => {
    try {
      const { id } = req.params;
      const results = await pool.query(
        `
      SELECT 
      "gonderi"."gonderi_id",
      "gonderi"."gonderi_tipi",
      "gonderi"."baslik",
      "spor"."spor_adi",
      "gonderi"."icerik",
      "gonderi"."gonderiTarihi",
      "kullanici"."kullanici_adi"
     FROM "cevap"
      INNER JOIN "gonderi" ON "gonderi"."gonderi_id" ="cevap"."gonderi_id"
      INNER JOIN "kullanici" ON "gonderi"."kullanici_id" = "kullanici"."kullanici_id"
      INNER JOIN "spor" ON "gonderi"."kategori" = "spor"."spor_id" WHERE "soru"='${id}';`
      );
      const answers = results.rows;
      res.status(200).json(answers);
    } catch (error) {
      console.log(error);
    }
  });
  
  router.delete("/:id", async (req, res) => {
    try {
      const { id } = req.params;
      const intId = parseInt(id);
      const result = await pool.query(
        `DELETE FROM "gonderi" WHERE "gonderi_id" = ${intId};`
      );
  
      res.status(200).json({ success: true });
    } catch (error) {
      console.log(error);
    }
  });
  
  module.exports = router;
  