<?php

namespace Controllers;

use Libs\ResponseLib;
use Models\DiscountModel;
use Psr\Http\Message\ServerRequestInterface;

class DiscountGetController
{
    private DiscountModel $model;

    public function __construct()
    {
        $this->model = new DiscountModel();
    }

    public function __invoke(ServerRequestInterface $request)
    {
        try {
            $rows = $this->model->getAll();
        } catch (\Throwable $e) {
            return ResponseLib::sendFail('Failed to fetch discounts: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
        }

        if (!$rows) {
            return ResponseLib::sendFail('Discounts Not Found', [], 'E_DATABASE')->withStatus(204);
        }

        return ResponseLib::sendOk($rows, 'S_DISCOUNTS');
    }
}
