<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\CreditPackage;
use App\Models\CreditRequest;
use Illuminate\Http\Request;

class CreditPackageController extends Controller
{
    /**
     * Display credit packages list
     */
    public function packagesIndex()
    {
        $packages = CreditPackage::orderBy('created_at', 'desc')->get();
        return view('admin.credit_packages.index', compact('packages'));
    }

    /**
     * Store new credit package
     */
    public function packagesStore(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'credits' => 'required|numeric|min:0',
            'price' => 'required|numeric|min:0',
            'description' => 'nullable|string',
        ]);

        CreditPackage::create([
            'name' => $request->name,
            'credits' => $request->credits,
            'price' => $request->price,
            'description' => $request->description,
            'is_active' => $request->has('is_active'),
        ]);

        return redirect()->route('admin.credit.packages')->with('success', 'Credit package created successfully!');
    }

    /**
     * Update credit package
     */
    public function packagesUpdate(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'credits' => 'required|numeric|min:0',
            'price' => 'required|numeric|min:0',
            'description' => 'nullable|string',
        ]);

        $package = CreditPackage::findOrFail($id);
        $package->update([
            'name' => $request->name,
            'credits' => $request->credits,
            'price' => $request->price,
            'description' => $request->description,
            'is_active' => $request->has('is_active'),
        ]);

        return redirect()->route('admin.credit.packages')->with('success', 'Credit package updated successfully!');
    }

    /**
     * Delete credit package
     */
    public function packagesDestroy($id)
    {
        $package = CreditPackage::findOrFail($id);
        $package->delete();

        return redirect()->route('admin.credit.packages')->with('success', 'Credit package deleted successfully!');
    }

    /**
     * Display credit requests list
     */
    public function requestsIndex()
    {
        $requests = CreditRequest::with(['reseller', 'creditPackage', 'approver'])
            ->orderBy('created_at', 'desc')
            ->get();
        return view('admin.credit_requests.index', compact('requests'));
    }

    /**
     * Approve credit request
     */
    public function requestsApprove(Request $request, $id)
    {
        $creditRequest = CreditRequest::with('reseller')->findOrFail($id);
        
        $creditRequest->update([
            'status' => 'approved',
            'approved_by' => auth('admins')->id(),
            'approved_at' => now(),
            'admin_notes' => $request->admin_notes,
        ]);

        // Add credits to reseller balance
        $reseller = $creditRequest->reseller;
        $reseller->increment('balance', $creditRequest->creditPackage->credits);

        return redirect()->route('admin.credit.requests')->with('success', 'Credit request approved and credits added to reseller!');
    }

    /**
     * Reject credit request
     */
    public function requestsReject(Request $request, $id)
    {
        $creditRequest = CreditRequest::findOrFail($id);
        
        $creditRequest->update([
            'status' => 'rejected',
            'approved_by' => auth('admins')->id(),
            'approved_at' => now(),
            'admin_notes' => $request->admin_notes,
        ]);

        return redirect()->route('admin.credit.requests')->with('success', 'Credit request rejected!');
    }
}