<?php
// app/controllers/scan_profiles.php

declare(strict_types=1);

use ScMedia\Http\Response;
use ScMedia\Http\Router;

final class ScanProfilesController extends BaseController {
  public static function register(Router $router, Container $container): void {
    $self = new self($container);
    $router->add('GET', '/api/scan-profiles', fn() => $self->listProfiles());
    $router->add('POST', '/api/scan-profiles', fn() => $self->createProfile());
    $router->add('PUT', '/api/scan-profiles/{id}', fn(array $params) => $self->updateProfile($params));
    $router->add('DELETE', '/api/scan-profiles/{id}', fn(array $params) => $self->deleteProfile($params));
    $router->add('POST', '/api/scan-profiles/reorder', fn() => $self->reorderProfiles());
  }

  private function listProfiles(): void {
    $rows = $this->services()['scan_profiles']->list();

    $out = [];
    foreach ($rows as $r) {
      $r['sort_order'] = (int)($r['sort_order'] ?? 0);
      $r['profile_type'] = (string)($r['profile_type'] ?? 'scan');
      $r['exclude_patterns'] = json_decode((string)$r['exclude_patterns_json'], true) ?: [];
      $r['include_ext'] = $r['include_ext_json'] ? (json_decode((string)$r['include_ext_json'], true) ?: []) : null;
      unset($r['exclude_patterns_json'], $r['include_ext_json']);
      $out[] = $r;
    }

    Response::json(['ok' => true, 'data' => $out]);
  }

  private function createProfile(): void {
    $logger = $this->logger();
    $body = read_json_body();
    try {
      $id = $this->services()['scan_profiles']->create($body);
      $logger->log('info', 'Scan profile created', ['id' => $id, 'name' => $body['name'] ?? null]);
      Response::json(['ok' => true, 'data' => ['id' => $id]]);
    } catch (Throwable $e) {
      $logger->log('error', 'Scan profile create failed', ['error' => $e->getMessage()]);
      Response::json(['ok' => false, 'error' => ['code' => 'VALIDATION_ERROR', 'message' => $e->getMessage()]], 400);
    }
  }

  private function updateProfile(array $params): void {
    $logger = $this->logger();
    $body = read_json_body();
    try {
      $this->services()['scan_profiles']->update((int)$params['id'], $body);
      $logger->log('info', 'Scan profile updated', ['id' => (int)$params['id'], 'name' => $body['name'] ?? null]);
      Response::json(['ok' => true]);
    } catch (Throwable $e) {
      $logger->log('error', 'Scan profile update failed', ['id' => (int)$params['id'], 'error' => $e->getMessage()]);
      Response::json(['ok' => false, 'error' => ['code' => 'VALIDATION_ERROR', 'message' => $e->getMessage()]], 400);
    }
  }

  private function deleteProfile(array $params): void {
    $logger = $this->logger();
    $this->services()['scan_profiles']->delete((int)$params['id']);
    $logger->log('info', 'Scan profile deleted', ['id' => (int)$params['id']]);
    Response::json(['ok' => true]);
  }

  private function reorderProfiles(): void {
    $logger = $this->logger();
    $body = read_json_body();
    $ids = is_array($body['ids'] ?? null) ? $body['ids'] : [];
    $this->services()['scan_profiles']->reorder($ids);
    $logger->log('info', 'Scan profiles reordered', ['count' => count($ids)]);
    Response::json(['ok' => true]);
  }
}
