<?php

declare(strict_types=1);

final class DeadlineScore
{
    private function expectedCompletionTime(string $step): int
    {
        return match ($step) {

            'start' => 600,               // 10 دقیقه

            'ign' => 900,                 // 15 دقیقه

            'game_id' => 1200,            // 20 دقیقه

            'force_join',
            'private_channel' => 900,

            'referral' => 21600,          // 6 ساعت

            'payment' => 43200,           // 12 ساعت

            default => 1800,
        };
    }

    public function calculate(array $row): array
    {
        if (empty($row['deadline'])) {
            return [
                'name' => 'deadline',
                'score' => 0,
                'reason' => null,
            ];
        }

        $deadline = strtotime($row['deadline']);

        if ($deadline === false) {
            return [
                'name' => 'deadline',
                'score' => 0,
                'reason' => null,
            ];
        }

        $remaining = $deadline - time();

        if ($remaining <= 0) {
            return [
                'name' => 'deadline',
                'score' => 0,
                'reason' => 'deadline_passed',
            ];
        }

        $expected = $this->expectedCompletionTime(
            $row['current_step']
        );

        if ($remaining <= $expected) {

            return [
                'name' => 'deadline',
                'score' => 100,
                'reason' => 'deadline_critical',
            ];
        }

        if ($remaining <= ($expected * 2)) {

            return [
                'name' => 'deadline',
                'score' => 80,
                'reason' => 'deadline_high',
            ];
        }

        if ($remaining <= ($expected * 4)) {

            return [
                'name' => 'deadline',
                'score' => 60,
                'reason' => 'deadline_medium',
            ];
        }

        if ($remaining <= 86400) {

            return [
                'name' => 'deadline',
                'score' => 35,
                'reason' => 'deadline_1d',
            ];
        }

        if ($remaining <= 604800) {

            return [
                'name' => 'deadline',
                'score' => 15,
                'reason' => 'deadline_7d',
            ];
        }

        return [
            'name' => 'deadline',
            'score' => 0,
            'reason' => null,
        ];
    }
}