import type { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import KeyValue from "App/Models/KeyValue";

export default class ValuesController {
  public async getKeyValue({ request, response }: HttpContextContract) {
    let data = await KeyValue.all();
    response.status(200).send(data);
  }

  public async create({ request, response }: HttpContextContract) {
    try {
      const value = await KeyValue.create({
        key: request.all().key,
        value: request.all().value,
        model: "All",
      });
      response.status(200).send("ADD VALUE SUCCESS!");
    } catch (error) {
      response.status(500).send("ADD VALUE FAIL!");
    }
  }

  public async store({}: HttpContextContract) {}

  public async show({}: HttpContextContract) {}

  public async edit({ request, response }: HttpContextContract) {
    try {
      const value = await KeyValue.findOrFail(request.all().id);
      value.value = request.all().value;
      await value.save();
      response.status(200).send("EDIT VALUE SUCCESS!");
    } catch (error) {
      response.status(500).send("EDIT VALUE FAIL!");
    }
  }

  public async update({}: HttpContextContract) {}

  public async destroy({ request, response }: HttpContextContract) {
    try {
      const value = await KeyValue.findOrFail(request.all().id);
      await value.delete();
      response.status(200).send("DELETE VALUE SUCCESS!");
    } catch (error) {
      response.status(500).send("DELETE VALUE FAIL!");
    }
  }
}
