<?php

namespace App\Repositories;

use App\Models\Backup;
use App\Models\Website;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;

class BackupRepository
{
    public function createForWebsite(Website $website, array $attributes): Backup
    {
        return $website->backups()->create($attributes);
    }

    public function paginate(array $filters, int $perPage = 15): LengthAwarePaginator
    {
        return Backup::query()
            ->with(['website', 'initiatedBy'])
            ->when($filters['status'] ?? null, fn ($query, $status) => $query->where('status', $status))
            ->when($filters['website'] ?? null, fn ($query, $websiteId) => $query->where('website_id', $websiteId))
            ->latest('created_at')
            ->paginate($perPage)
            ->withQueryString();
    }

    public function recent(int $limit = 6): Collection
    {
        return Backup::query()
            ->with('website')
            ->latest('created_at')
            ->limit($limit)
            ->get();
    }
}
