<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Session;
use App\Services\TarifClientService;

final class TarifClientController extends Controller
{
    public function store(): void
    {
        $clientId = (int) Request::post('client_id', 0);

        try {
            $clientId = (new TarifClientService())->saveForClient(Request::all());
            Session::flash('success', 'Tarif client enregistré.');
        } catch (\Throwable $exception) {
            Session::flash('error', $exception->getMessage());
        }

        $this->redirect('clients_show&id=' . $clientId);
    }

    public function delete(): void
    {
        $clientId = (int) Request::post('client_id', 0);
        $id = (int) Request::post('id', 0);

        try {
            (new TarifClientService())->deleteForClient($id, $clientId);
            Session::flash('success', 'Tarif client supprimé.');
        } catch (\Throwable $exception) {
            Session::flash('error', $exception->getMessage());
        }

        $this->redirect('clients_show&id=' . $clientId);
    }
}
