<?php

namespace App\Http\Controllers\Reseller;

use App\Http\Controllers\Controller;
use App\Models\Invoice;
use App\Models\Package;

class PackageController extends Controller
{
    public function index()
    {
        $packages = Package::where('is_active', true)->orderBy('blogs_count')->get();
        return view('reseller.packages.index', compact('packages'));
    }

    public function buy(Package $package)
    {
        $invoice = Invoice::create([
            'user_id'    => auth()->id(),
            'package_id' => $package->id,
            'amount'     => $package->price,
            'currency'   => config('paypal.currency', 'USD'),
            'status'     => 'unpaid',
            'due_date'   => now()->addDays(7),
            'notes'      => 'Auto-generated for package: ' . $package->name,
        ]);

        return redirect()->route('paypal.process', $invoice);
    }
}
