<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Models\ReviewPack;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class PruneReviewPacksCommand extends Command
{
    /**
     * @var string
     */
    protected $signature = 'tenantpilot:review-pack:prune {--hard-delete : Hard-delete expired packs past grace period}';

    /**
     * @var string
     */
    protected $description = 'Expire review packs past retention and optionally hard-delete expired rows past grace period';

    public function handle(): int
    {
        $expired = $this->expireReadyPacks();
        $hardDeleted = 0;

        if ($this->option('hard-delete')) {
            $hardDeleted = $this->hardDeleteExpiredPacks();
        }

        $this->info("{$expired} pack(s) expired, {$hardDeleted} pack(s) hard-deleted.");

        return self::SUCCESS;
    }

    /**
     * Transition ready packs past retention to expired and delete their files.
     */
    private function expireReadyPacks(): int
    {
        $packs = ReviewPack::query()
            ->ready()
            ->pastRetention()
            ->get();

        $disk = Storage::disk('exports');
        $count = 0;

        foreach ($packs as $pack) {
            /** @var ReviewPack $pack */
            if ($pack->file_path && $disk->exists($pack->file_path)) {
                $disk->delete($pack->file_path);
            }

            $pack->update(['status' => ReviewPack::STATUS_EXPIRED]);
            $count++;
        }

        return $count;
    }

    /**
     * Hard-delete expired packs that are past the grace period.
     */
    private function hardDeleteExpiredPacks(): int
    {
        $graceDays = (int) config('tenantpilot.review_pack.hard_delete_grace_days', 30);

        $cutoff = now()->subDays($graceDays);

        return ReviewPack::query()
            ->expired()
            ->where('updated_at', '<', $cutoff)
            ->delete();
    }
}
