<?php
// app/bootstrap.php

declare(strict_types=1);

if (!function_exists('load_env_file')) {
  function load_env_file(string $path): void {
    if (!is_file($path)) {
      return;
    }

    $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    if (!is_array($lines)) {
      return;
    }

    foreach ($lines as $line) {
      $line = trim($line);
      if ($line === '' || str_starts_with($line, '#')) {
        continue;
      }

      $parts = explode('=', $line, 2);
      if (count($parts) !== 2) {
        continue;
      }

      $key = trim($parts[0]);
      if ($key === '' || getenv($key) !== false) {
        continue;
      }

      $value = trim($parts[1]);
      if ($value !== '' && ($value[0] === '"' || $value[0] === "'")) {
        $quote = $value[0];
        if (str_ends_with($value, $quote)) {
          $value = substr($value, 1, -1);
        }
      }

      putenv("{$key}={$value}");
      $_ENV[$key] = $value;
      $_SERVER[$key] = $value;
    }
  }
}

load_env_file(__DIR__ . '/../.env');

$config = require __DIR__ . '/../config/config.php';

require_once __DIR__ . '/db/Db.php';
require_once __DIR__ . '/db/SchemaTool.php';

require_once __DIR__ . '/http/Response.php';
require_once __DIR__ . '/http/Router.php';

require_once __DIR__ . '/services/SettingsService.php';
require_once __DIR__ . '/services/ScanProfilesService.php';
require_once __DIR__ . '/services/LayoutService.php';
require_once __DIR__ . '/services/PathTool.php';
require_once __DIR__ . '/services/JobsService.php';
require_once __DIR__ . '/services/ScannerService.php';
require_once __DIR__ . '/services/HttpClient.php';
require_once __DIR__ . '/services/MkvToolnixService.php';
require_once __DIR__ . '/services/MediaLibraryService.php';
require_once __DIR__ . '/services/MediaApplyService.php';
require_once __DIR__ . '/services/LogService.php';
require_once __DIR__ . '/services/ShellTool.php';
require_once __DIR__ . '/services/MetadataService.php';
require_once __DIR__ . '/services/ExportService.php';
require_once __DIR__ . '/services/TransmissionService.php';
require_once __DIR__ . '/services/SourcesService.php';
require_once __DIR__ . '/services/AuthService.php';

require_once __DIR__ . '/services/metadata/MetadataProvider.php';
require_once __DIR__ . '/services/metadata/OmdbProvider.php';
require_once __DIR__ . '/services/metadata/TvdbProvider.php';

require_once __DIR__ . '/services/export/ExporterInterface.php';
require_once __DIR__ . '/services/export/KodiExporter.php';
require_once __DIR__ . '/services/export/JellyfinExporter.php';

$db = new \ScMedia\Db\Db($config['db']['dsn'], $config['db']['user'], $config['db']['pass']);

$settings = new \ScMedia\Services\SettingsService($db, $config);
$scanProfiles = new \ScMedia\Services\ScanProfilesService($db, $settings, $config);
$logger = new \ScMedia\Services\LogService($db, $settings);
$jobs = new \ScMedia\Services\JobsService($db, $logger);
$shell = new \ScMedia\Services\ShellTool();
$http = new \ScMedia\Services\HttpClient();
$mkvtoolnix = new \ScMedia\Services\MkvToolnixService($db, $settings, $logger, $shell);
$metadataProviders = [
  new \ScMedia\Services\Metadata\OmdbProvider($http, $logger),
  new \ScMedia\Services\Metadata\TvdbProvider($http, $logger),
];
$metadata = new \ScMedia\Services\MetadataService($db, $settings, $metadataProviders, $logger);
$mediaLibrary = new \ScMedia\Services\MediaLibraryService($db, $settings, $metadata);
$transmission = new \ScMedia\Services\TransmissionService($http, $settings, $logger);
$sources = new \ScMedia\Services\SourcesService($transmission, $settings);
$auth = new \ScMedia\Services\AuthService($db, $config);
$exporters = [
  new \ScMedia\Services\Export\KodiExporter(),
  new \ScMedia\Services\Export\JellyfinExporter(),
];
$exportService = new \ScMedia\Services\ExportService($settings, $exporters);
$mediaApply = new \ScMedia\Services\MediaApplyService($mediaLibrary, $mkvtoolnix, $jobs, $metadata, $exportService);

$services = [
  'db' => $db,
  'config' => $config,
  'schema' => new \ScMedia\Db\SchemaTool($db, __DIR__ . '/../sql/schema.sql'),

  'settings' => $settings,
  'scan_profiles' => $scanProfiles,
  'layout' => new \ScMedia\Services\LayoutService($db, $config),
  'path_tool' => new \ScMedia\Services\PathTool(),

  'jobs' => $jobs,
  'logger' => $logger,
  'shell' => $shell,
  'mkvtoolnix' => $mkvtoolnix,
  'media_library' => $mediaLibrary,
  'media_apply' => $mediaApply,
  'metadata' => $metadata,
  'export_service' => $exportService,
  'transmission' => $transmission,
  'sources' => $sources,
  'auth' => $auth,
  'scanner' => new \ScMedia\Services\ScannerService($db, $settings, $scanProfiles, $jobs, $mkvtoolnix, $mediaLibrary),
];

return $services;
