<?php

namespace FSPoster\App\TelegramNotification\App\Controllers;

use FSPoster\App\Providers\Core\Container;
use FSPoster\App\Providers\Core\RestRequest;
use FSPoster\App\TelegramNotification\App\DTOs\Request\TelegramNotificationRequest;
use FSPoster\App\TelegramNotification\App\Services\TelegramNotificationService;

class TelegramNotificationController
{
    private TelegramNotificationService $service;

    public function __construct(TelegramNotificationService $service)
    {
        $this->service = $service;
    }

    public function view(RestRequest $request): array
    {
        $id = $request->require('id', RestRequest::TYPE_INTEGER);

        $response = $this->service->getViewData($id);

        return $response->jsonSerialize();
    }

    public function edit(RestRequest $request): array
    {
        $id = $request->param('id', 0, RestRequest::TYPE_INTEGER);
        $isActive = $request->param('is_active', 1, RestRequest::TYPE_INTEGER);
        $channels = $request->param('channels', [], RestRequest::TYPE_ARRAY);
        $message = $request->param('message', '', RestRequest::TYPE_STRING);

        $telegramRequest = Container::get(TelegramNotificationRequest::class);
        $telegramRequest->setChannels($channels);
        $telegramRequest->setMessage($message);

        $this->service->updateAction($id, $telegramRequest, $isActive);

        return [];
    }
}
