<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Package;
use Illuminate\Http\Request;

class PackageController extends Controller
{
    public $route = 'admin.package';

    /**
     * List all packages
     */
    public function index()
    {
        $packages = Package::latest()->get();
        return view('admin.pages.package.index', compact('packages'));
    }

    /**
     * Show create/edit form
     */
    public function create($id = null)
    {
        $data = $id ? Package::find($id) : null;
        return view('admin.pages.package.insert', compact('data'));
    }

    /**
     * View single package
     */
    public function view($id)
    {
        $data = Package::findOrFail($id);
        return view('admin.pages.package.view', compact('data'));
    }

    /**
     * Insert or update package
     */
    public function insert_or_update(Request $request)
    {
        $rules = [
            'name' => 'required|string|max:255',
            'label' => 'required|string|max:255',
            'tab' => 'required|string|max:50',
            'price' => 'required|numeric|min:0',
            'validity' => 'required|numeric|min:1',
           
            'daily_watch_limit' => 'required|numeric|min:1',
            'commission_with_avg_amount' => 'required|numeric|min:0',
          
            'ref1' => 'required|numeric|min:0',
            'ref2' => 'required|numeric|min:0',
            'ref3' => 'required|numeric|min:0',
            'status' => 'required|in:active,inactive',
            'photo' => $request->id ? 'nullable|image|mimes:jpeg,png,jpg,gif,webp|max:2048' : 'required|image|mimes:jpeg,png,jpg,gif,webp|max:2048',
        ];

        $request->validate($rules);

        $package = $request->id ? Package::findOrFail($request->id) : new Package();

        // Handle image upload
        if ($request->hasFile('photo')) {
            $path = uploadImage(false, $request, 'photo', 'upload/package/', 200, 200, $package->photo ?? null);
            $package->photo = $path;
        }

        $package->name = $request->name;
        $package->label = $request->label;
        $package->tab = $request->tab;
        $package->price = $request->price;
        $package->validity = $request->validity;
       
        $package->daily_watch_limit = $request->daily_watch_limit;
        $package->commission_with_avg_amount = $request->commission_with_avg_amount;
       
        $package->ref1 = $request->ref1;
        $package->ref2 = $request->ref2;
        $package->ref3 = $request->ref3;
        $package->status = $request->status ?? 'active';
        $package->is_default = $request->is_default ?? 0;
        $package->desc = $request->desc ?? null;

        $package->save();

        $msg = $request->id ? 'Package updated successfully.' : 'Package created successfully.';
        return redirect()->route($this->route.'.index')->with('success', $msg);
    }

    /**
     * Delete package
     */
    public function delete($id)
    {
        $package = Package::findOrFail($id);

        if ($package->photo) {
            deleteImage($package->photo);
        }

        $package->delete();
        return redirect()->route($this->route.'.index')->with('success', 'Package deleted successfully.');
    }
}
