<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Auth;
use App\Core\Controller;
use App\Helpers\Validator;
use App\Services\InstallerService;

class InstallerController extends Controller
{
    public function index(): void
    {
        Auth::requireLogin();
        $this->view('installer/index', []);
    }

    public function install(): void
    {
        Auth::requireLogin();

        $validator = new Validator();

        // FTP
        $validator
            ->required('ftp_host', $this->input('ftp_host'), 'Host FTP')
            ->required('ftp_user', $this->input('ftp_user'), 'Użytkownik FTP')
            ->required('ftp_pass', $this->input('ftp_pass'), 'Hasło FTP')
            ->required('ftp_path', $this->input('ftp_path'), 'Ścieżka FTP');

        // Database
        $validator
            ->required('db_host', $this->input('db_host'), 'Host bazy danych')
            ->required('db_name', $this->input('db_name'), 'Nazwa bazy danych')
            ->required('db_user', $this->input('db_user'), 'Użytkownik bazy danych')
            ->required('db_pass', $this->input('db_pass'), 'Hasło bazy danych');

        // WordPress admin
        $validator
            ->required('site_url', $this->input('site_url'), 'URL strony')
            ->url('site_url', $this->input('site_url'), 'URL strony')
            ->required('site_title', $this->input('site_title'), 'Tytuł strony')
            ->required('admin_user', $this->input('admin_user'), 'Login administratora')
            ->required('admin_pass', $this->input('admin_pass'), 'Hasło administratora')
            ->minLength('admin_pass', $this->input('admin_pass'), 8, 'Hasło administratora')
            ->required('admin_email', $this->input('admin_email'), 'E-mail administratora')
            ->email('admin_email', $this->input('admin_email'), 'E-mail administratora');

        if (!$validator->isValid()) {
            $this->json(['success' => false, 'message' => $validator->getFirstError()]);
            return;
        }

        $progressId = $this->input('progress_id', '');
        if (empty($progressId) || !preg_match('/^[a-zA-Z0-9]{10,30}$/', $progressId)) {
            $this->json(['success' => false, 'message' => 'Nieprawidłowy identyfikator postępu.']);
            return;
        }

        $config = [
            'ftp_host'    => $this->input('ftp_host'),
            'ftp_user'    => $this->input('ftp_user'),
            'ftp_pass'    => $this->input('ftp_pass'),
            'ftp_path'    => rtrim($this->input('ftp_path'), '/'),
            'ftp_port'    => (int) ($this->input('ftp_port', '21')),
            'ftp_ssl'     => (bool) $this->input('ftp_ssl'),
            'db_host'     => $this->input('db_host'),
            'db_name'     => $this->input('db_name'),
            'db_user'     => $this->input('db_user'),
            'db_pass'     => $this->input('db_pass'),
            'db_prefix'   => $this->input('db_prefix', 'wp_'),
            'site_url'    => rtrim($this->input('site_url'), '/'),
            'site_title'  => $this->input('site_title'),
            'admin_user'  => $this->input('admin_user'),
            'admin_pass'  => $this->input('admin_pass'),
            'admin_email' => $this->input('admin_email'),
            'language'    => $this->input('language', 'pl_PL'),
        ];

        $installer = new InstallerService();
        $result = $installer->install($config, $progressId);

        // Cleanup progress file after completion
        InstallerService::cleanupProgress($progressId);

        $this->json($result);
    }

    public function status(string $id): void
    {
        Auth::requireLogin();

        if (!preg_match('/^[a-zA-Z0-9]{10,30}$/', $id)) {
            $this->json(['percent' => 0, 'message' => 'Nieprawidłowy ID', 'status' => 'failed']);
            return;
        }

        $progress = InstallerService::getProgress($id);

        if ($progress === null) {
            $this->json(['percent' => 0, 'message' => 'Oczekiwanie na start...', 'status' => 'waiting']);
            return;
        }

        $this->json($progress);
    }
}
