<?php

namespace Services;

use Models\CprMonitoringModel;

class CprMonitoringService
{
    private CprMonitoringModel $model;

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

    public function create(int $cprId, bool $preview, string $description, string $link): array
    {
        if ($cprId <= 0) {
            throw new \InvalidArgumentException('Invalid cpr_id');
        }

        $description = trim($description);
        $link = trim($link);

        if ($description === '') {
            throw new \InvalidArgumentException('Invalid description');
        }

        if ($link === '') {
            throw new \InvalidArgumentException('Invalid link');
        }

        return $this->model->create($cprId, $preview, $description, $link);
    }

    public function getById(int $id): ?array
    {
        if ($id <= 0) {
            throw new \InvalidArgumentException('Invalid id');
        }

        return $this->model->getById($id);
    }

    public function listByCprId(int $cprId): array
    {
        if ($cprId <= 0) {
            throw new \InvalidArgumentException('Invalid cpr_id');
        }

        return $this->model->listByCprId($cprId);
    }

    public function update(int $id, ?bool $preview = null, ?string $description = null, ?string $link = null): ?array
    {
        if ($id <= 0) {
            throw new \InvalidArgumentException('Invalid id');
        }

        if ($description !== null) {
            $description = trim($description);
            if ($description === '') {
                throw new \InvalidArgumentException('Invalid description');
            }
        }

        if ($link !== null) {
            $link = trim($link);
            if ($link === '') {
                throw new \InvalidArgumentException('Invalid link');
            }
        }

        return $this->model->update($id, $preview, $description, $link);
    }

    public function delete(int $id): bool
    {
        if ($id <= 0) {
            throw new \InvalidArgumentException('Invalid id');
        }

        return $this->model->delete($id);
    }
}
