<?php

namespace App\Core\Repositories;

use App\Models\RecurringInvoice;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

/**
 * Recurring Invoice Repository
 * Handles all database operations for recurring invoices
 */
class RecurringInvoiceRepository
{
    /**
     * Get all recurring invoices
     */
    public function getAll(): Collection
    {
        return RecurringInvoice::with(['customer', 'invoices'])->get();
    }

    /**
     * Get paginated recurring invoices
     */
    public function getPaginated(int $perPage = 15, array $filters = []): LengthAwarePaginator
    {
        $query = RecurringInvoice::with(['customer', 'invoices']);

        // Apply filters
        if (isset($filters['status'])) {
            $query->where('status', $filters['status']);
        }

        if (isset($filters['customer_id'])) {
            $query->where('customer_id', $filters['customer_id']);
        }

        return $query->orderBy('created_at', 'desc')->paginate($perPage);
    }

    /**
     * Get recurring invoice by ID
     */
    public function getById(int $id): ?RecurringInvoice
    {
        return RecurringInvoice::with(['customer', 'invoices'])->find($id);
    }

    /**
     * Create a new recurring invoice
     */
    public function create(array $data): RecurringInvoice
    {
        return RecurringInvoice::create($data);
    }

    /**
     * Update a recurring invoice
     */
    public function update(RecurringInvoice $recurringInvoice, array $data): bool
    {
        return $recurringInvoice->update($data);
    }

    /**
     * Delete a recurring invoice
     */
    public function delete(RecurringInvoice $recurringInvoice): bool
    {
        return $recurringInvoice->delete();
    }
}
