<?php

namespace App\Http\Controllers\Api;

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

class PackageController extends Controller
{
    public function index()
    {
        return Package::latest()->paginate(25);
    }

    public function store(Request $request)
    {
        return Package::create($request->validate([
            'name' => ['required', 'string'],
            'code' => ['required', 'string', 'unique:packages,code'],
            'description' => ['nullable', 'string'],
            'monthly_price' => ['required', 'numeric'],
            'yearly_price' => ['required', 'numeric'],
            'employee_limit' => ['required', 'integer'],
            'branch_limit' => ['required', 'integer'],
            'device_limit' => ['required', 'integer'],
            'trial_days' => ['required', 'integer'],
            'features' => ['array'],
            'is_active' => ['boolean'],
        ]));
    }

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

    public function update(Request $request, Package $package)
    {
        $package->update($request->all());

        return $package->fresh();
    }

    public function destroy(Package $package)
    {
        $package->update(['is_active' => false]);

        return response()->noContent();
    }
}
