<?php

namespace EasyLinkAutomations\HTTP\Controllers\Actions;

if (!defined('ABSPATH')) {
    exit;
}

use EasyLinkAutomations\Actions\Freshservice\FreshserviceRequestHelper;
use EasyLinkAutomations\HTTP\Request\Request;
use EasyLinkAutomations\HTTP\Response;

final class FreshserviceController
{
    public function agents(Request $request): Response { return $this->list($request, 'agents', 'agents', ['first_name', 'last_name', 'email']); }
    public function agentGroups(Request $request): Response { return $this->list($request, 'groups', 'agent_groups', ['name'], 'groups'); }
    public function agentRoles(Request $request): Response { return $this->list($request, 'roles', 'agent_roles', ['name'], 'roles'); }
    public function announcements(Request $request): Response { return $this->list($request, 'announcements', 'announcements', ['title']); }
    public function assetTypes(Request $request): Response { return $this->list($request, 'asset_types', 'asset_types', ['name']); }
    public function changes(Request $request): Response { return $this->list($request, 'changes', 'changes', ['subject']); }
    public function departments(Request $request): Response { return $this->list($request, 'departments', 'departments', ['name']); }
    public function locations(Request $request): Response { return $this->list($request, 'locations', 'locations', ['name']); }
    public function problems(Request $request): Response { return $this->list($request, 'problems', 'problems', ['subject']); }
    public function products(Request $request): Response { return $this->list($request, 'products', 'products', ['name']); }
    public function releases(Request $request): Response { return $this->list($request, 'releases', 'releases', ['subject']); }
    public function requesters(Request $request): Response { return $this->list($request, 'requesters', 'requesters', ['first_name', 'last_name', 'primary_email']); }
    public function requesterGroups(Request $request): Response { return $this->list($request, 'requester_groups', 'requester_groups', ['name']); }
    public function software(Request $request): Response { return $this->list($request, 'applications', 'software', ['name'], 'applications'); }
    public function tickets(Request $request): Response { return $this->list($request, 'tickets', 'tickets', ['subject']); }

    private function list(Request $request, string $path, string $key, array $labelFields, string $payloadKey = ''): Response
    {
        $connectionId = (int) $request->get('connection_id');
        if (!$connectionId) {
            return Response::error('Connection ID is required', 400);
        }

        $result = FreshserviceRequestHelper::request($connectionId, 'GET', $path, [], ['per_page' => 100, 'page' => 1]);
        if (!$result['success']) {
            return Response::error($result['message'], 400);
        }

        $payload = is_array($result['data']) ? $result['data'] : [];
        $payloadKey = $payloadKey !== '' ? $payloadKey : $key;
        $records = $payload[$payloadKey] ?? [];
        $items = [];
        foreach (is_array($records) ? $records : [] as $record) {
            $id = (string) ($record['id'] ?? $record['display_id'] ?? '');
            if ($id === '') {
                continue;
            }
            $parts = [];
            foreach ($labelFields as $field) {
                if (!empty($record[$field])) {
                    $parts[] = (string) $record[$field];
                }
            }
            $items[] = ['value' => $id, 'label' => $parts ? implode(' ', $parts) : $key . ' #' . $id];
        }

        return Response::success([$key => $items]);
    }
}
