<?php
// app/services/ExportService.php

declare(strict_types=1);

namespace ScMedia\Services;

use ScMedia\Services\Export\ExporterInterface;

final class ExportService {
  private SettingsService $settings;
  /** @var ExporterInterface[] */
  private array $exporters;

  /**
   * @param ExporterInterface[] $exporters
   */
  public function __construct(SettingsService $settings, array $exporters) {
    $this->settings = $settings;
    $this->exporters = $exporters;
  }

  public function getEnabledExporters(): array {
    $all = $this->settings->getAll();
    $cfg = $all['exports'] ?? [];
    $enabled = [];
    foreach ($this->exporters as $exp) {
      $id = $exp->id();
      if (!empty($cfg[$id]['enabled'])) {
        $enabled[$id] = $exp;
      }
    }
    return $enabled;
  }

  public function exportMovie(string $filePath, array $meta): array {
    $enabled = $this->getEnabledExporters();
    $out = [];
    foreach ($enabled as $id => $exp) {
      if (!$exp->supportsKind('movie')) continue;
      $out[$id] = $exp->exportMovie($filePath, $meta);
    }
    return $out;
  }

  public function exportSeries(string $seriesPath, array $meta): array {
    $enabled = $this->getEnabledExporters();
    $out = [];
    foreach ($enabled as $id => $exp) {
      if (!$exp->supportsKind('series')) continue;
      $out[$id] = $exp->exportSeries($seriesPath, $meta);
    }
    return $out;
  }
}
