<?php

namespace App\Http\Controllers\Admin;

use App\Core\Repositories\CustomerRepository;
use App\Core\Repositories\RecurringInvoiceRepository;
use App\Core\Services\Recurring\RecurringInvoiceService;
use App\Http\Controllers\Controller;
use App\Models\RecurringInvoice;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class RecurringInvoiceController extends Controller
{
    public function __construct(
        private RecurringInvoiceService $recurringInvoiceService,
        private CustomerRepository $customerRepository,
        private RecurringInvoiceRepository $recurringInvoiceRepository
    ) {}

    /**
     * Display a listing of recurring invoices
     */
    public function index(Request $request): View
    {
        $filters = [
            'status' => $request->get('status'),
            'customer_id' => $request->get('customer_id'),
        ];

        $recurringInvoices = $this->recurringInvoiceRepository->getPaginated(15, $filters);
        $customers = $this->customerRepository->getAll();

        return view('admin.recurring-invoices.index', [
            'recurringInvoices' => $recurringInvoices,
            'customers' => $customers,
            'filters' => $filters,
        ]);
    }

    /**
     * Show the form for creating a new recurring invoice
     */
    public function create(): View
    {
        $customers = $this->customerRepository->getAll();

        return view('admin.recurring-invoices.create', [
            'customers' => $customers,
        ]);
    }

    /**
     * Store a newly created recurring invoice
     */
    public function store(Request $request): RedirectResponse
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'customer_id' => 'required|exists:customers,id',
            'frequency' => 'required|in:daily,weekly,biweekly,monthly,quarterly,semiannually,annually',
            'start_date' => 'required|date',
            'end_date' => 'nullable|date|after:start_date',
            'occurrences' => 'nullable|integer|min:1',
            'invoice_data' => 'required|array',
            'invoice_data.items' => 'required|array|min:1',
            'invoice_data.items.*.description' => 'required|string',
            'invoice_data.items.*.quantity' => 'required|numeric|min:0.01',
            'invoice_data.items.*.unit_price' => 'required|numeric|min:0',
            'invoice_data.items.*.tax_rate' => 'nullable|numeric|min:0|max:100',
        ]);

        $recurringInvoice = $this->recurringInvoiceService->create($validated);

        return redirect()
            ->route('admin.recurring-invoices.show', $recurringInvoice)
            ->with('success', 'Recurring invoice created successfully.');
    }

    /**
     * Display the specified recurring invoice
     */
    public function show(RecurringInvoice $recurringInvoice): View
    {
        $recurringInvoice->load(['customer', 'invoices']);

        return view('admin.recurring-invoices.show', [
            'recurringInvoice' => $recurringInvoice,
        ]);
    }

    /**
     * Pause a recurring invoice
     */
    public function pause(RecurringInvoice $recurringInvoice): RedirectResponse
    {
        $recurringInvoice->update(['status' => 'paused']);

        return redirect()
            ->route('admin.recurring-invoices.show', $recurringInvoice)
            ->with('success', 'Recurring invoice paused successfully.');
    }

    /**
     * Resume a recurring invoice
     */
    public function resume(RecurringInvoice $recurringInvoice): RedirectResponse
    {
        $recurringInvoice->update(['status' => 'active']);

        return redirect()
            ->route('admin.recurring-invoices.show', $recurringInvoice)
            ->with('success', 'Recurring invoice resumed successfully.');
    }

    /**
     * Cancel a recurring invoice
     */
    public function cancel(RecurringInvoice $recurringInvoice): RedirectResponse
    {
        $recurringInvoice->update(['status' => 'cancelled']);

        return redirect()
            ->route('admin.recurring-invoices.index')
            ->with('success', 'Recurring invoice cancelled successfully.');
    }
}
