<?php
// app/controllers/metadata.php

declare(strict_types=1);

use ScMedia\Http\Response;
use ScMedia\Http\Router;
use ScMedia\Services\MetadataService;

final class MetadataController extends BaseController {
  public static function register(Router $router, Container $container): void {
    $self = new self($container);
    $router->add('POST', '/api/metadata/search', fn() => $self->search());
    $router->add('POST', '/api/metadata/select', fn() => $self->select());
    $router->add('POST', '/api/metadata/manual', fn() => $self->manual());
    $router->add('POST', '/api/metadata/manual/clear', fn() => $self->clearManual());
  }

  private function search(): void {
    /** @var MetadataService $metadata */
    $metadata = $this->services()['metadata'];
    $logger = $this->logger();
    $body = read_json_body();
    $query = (string)($body['query'] ?? '');
    $type = (string)($body['type'] ?? 'movie');
    $year = isset($body['year']) ? (int)$body['year'] : null;
    $type = ($type === 'series') ? 'series' : 'movie';
    $results = $metadata->search($query, $type, $year);
    $logger->log('info', 'Metadata search', ['query' => $query, 'type' => $type, 'year' => $year, 'results' => count($results)]);
    Response::json(['ok' => true, 'data' => $results]);
  }

  private function select(): void {
    /** @var MetadataService $metadata */
    $metadata = $this->services()['metadata'];
    $logger = $this->logger();
    $body = read_json_body();
    $kind = (string)($body['subject_kind'] ?? '');
    $key = (string)($body['subject_key'] ?? '');
    $sel = is_array($body['selection'] ?? null) ? $body['selection'] : [];
    if ($kind === '' || $key === '') {
      Response::json(['ok' => false, 'error' => ['code' => 'SUBJECT_REQUIRED', 'message' => 'subject required']], 400);
      return;
    }
    $metadata->saveSelection($kind, $key, $sel);
    $metaRow = $metadata->getSubjectMeta($kind, $key);
    $display = $metadata->formatDisplay($metaRow);
    $logger->log('info', 'Metadata selected', ['subject_kind' => $kind, 'subject_key' => $key, 'provider' => $sel['provider'] ?? null, 'provider_id' => $sel['provider_id'] ?? null]);
    Response::json(['ok' => true, 'data' => $display]);
  }

  private function manual(): void {
    /** @var MetadataService $metadata */
    $metadata = $this->services()['metadata'];
    $logger = $this->logger();
    $body = read_json_body();
    $kind = (string)($body['subject_kind'] ?? '');
    $key = (string)($body['subject_key'] ?? '');
    $title = (string)($body['title'] ?? '');
    $year = isset($body['year']) && $body['year'] !== '' ? (int)$body['year'] : null;
    if ($kind === '' || $key === '') {
      Response::json(['ok' => false, 'error' => ['code' => 'SUBJECT_REQUIRED', 'message' => 'subject required']], 400);
      return;
    }
    $metadata->saveManual($kind, $key, $title, $year);
    $metaRow = $metadata->getSubjectMeta($kind, $key);
    $display = $metadata->formatDisplay($metaRow);
    $logger->log('info', 'Metadata manual override', ['subject_kind' => $kind, 'subject_key' => $key, 'title' => $title, 'year' => $year]);
    Response::json(['ok' => true, 'data' => $display]);
  }

  private function clearManual(): void {
    /** @var MetadataService $metadata */
    $metadata = $this->services()['metadata'];
    $logger = $this->logger();
    $body = read_json_body();
    $kind = (string)($body['subject_kind'] ?? '');
    $key = (string)($body['subject_key'] ?? '');
    if ($kind === '' || $key === '') {
      Response::json(['ok' => false, 'error' => ['code' => 'SUBJECT_REQUIRED', 'message' => 'subject required']], 400);
      return;
    }
    $metadata->clearManual($kind, $key);
    $metaRow = $metadata->getSubjectMeta($kind, $key);
    $display = $metadata->formatDisplay($metaRow);
    $logger->log('info', 'Metadata manual override cleared', ['subject_kind' => $kind, 'subject_key' => $key]);
    Response::json(['ok' => true, 'data' => $display]);
  }
}
