<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Photo;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

class DeleteExpiredPhotos extends Command
{
    protected $signature = 'photos:delete-expired';
    protected $description = 'Menghapus foto yang sudah lebih dari 7 hari dari tanggal upload';

    public function handle()
    {
        $expiredPhotos = Photo::where('created_at', '<', Carbon::now()->subDays(7))->get();

        foreach ($expiredPhotos as $photo) {
            // Hapus file dari penyimpanan
            Storage::delete($photo->file_path);

            // Hapus dari database
            $photo->delete();
        }

        $this->info(count($expiredPhotos) . ' foto telah dihapus.');
    }
}
