<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\PackageResource;
use App\Http\Resources\PatientPackageResource;
use App\Models\Package;
use App\Models\PatientPackage;
use Illuminate\Http\Request;

class PackageController extends Controller
{
    public function index(Request $request)
    {
        $query = Package::with('currency')->where('is_active', true);

        if ($request->has('search')) {
            $query->where('name', 'like', "%{$request->search}%");
        }

        $packages = $query->orderBy('name')->paginate($request->per_page ?? 20);

        return PackageResource::collection($packages);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|string|max:255',
            'number_of_sessions' => 'required|integer|min:1',
            'price' => 'required|numeric|min:0',
            'currency_id' => 'required|exists:currencies,id',
            'validity_days' => 'required|integer|min:1',
            'description' => 'nullable|string',
        ]);

        $package = Package::create($data);

        return new PackageResource($package);
    }

    public function show(Package $package)
    {
        return new PackageResource($package);
    }

    public function update(Request $request, Package $package)
    {
        $data = $request->validate([
            'name' => 'sometimes|string|max:255',
            'number_of_sessions' => 'sometimes|integer|min:1',
            'price' => 'sometimes|numeric|min:0',
            'currency_id' => 'sometimes|exists:currencies,id',
            'validity_days' => 'sometimes|integer|min:1',
            'description' => 'nullable|string',
            'is_active' => 'sometimes|boolean',
        ]);

        $package->update($data);

        return new PackageResource($package);
    }

    public function destroy(Package $package)
    {
        $package->delete();
        return response()->json(['message' => 'Package deleted successfully']);
    }

    // Patient Package Assignment
    public function assignToPatient(Request $request)
    {
        $data = $request->validate([
            'patient_id' => 'required|exists:patients,id',
            'package_id' => 'required|exists:packages,id',
            'start_date' => 'required|date',
        ]);

        $package = Package::findOrFail($data['package_id']);

        $patientPackage = PatientPackage::create([
            'patient_id' => $data['patient_id'],
            'package_id' => $data['package_id'],
            'total_sessions' => $package->number_of_sessions,
            'remaining_sessions' => $package->number_of_sessions,
            'start_date' => $data['start_date'],
            'end_date' => \Carbon\Carbon::parse($data['start_date'])->addDays($package->validity_days),
            'status' => 'active',
        ]);

        return new PatientPackageResource($patientPackage);
    }

    public function patientPackages(Request $request)
    {
        $query = PatientPackage::with(['patient', 'package']);

        if ($request->has('patient_id')) {
            $query->where('patient_id', $request->patient_id);
        }

        if ($request->has('status')) {
            $query->where('status', $request->status);
        }

        $packages = $query->orderByDesc('created_at')->paginate($request->per_page ?? 20);

        return PatientPackageResource::collection($packages);
    }
}
