<?php

namespace App\Http\Controllers\Api;

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

class ClientController extends Controller
{
    public function index()
    {
        return Tenant::with('package')->latest()->paginate(25);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'package_id' => ['nullable', 'integer'],
            'name' => ['required', 'string', 'max:180'],
            'slug' => ['required', 'string', 'max:120', 'unique:tenants,slug'],
            'email' => ['required', 'email'],
            'phone' => ['nullable', 'string'],
            'status' => ['required', 'in:trial,active,suspended,expired'],
            'timezone' => ['required', 'string'],
            'currency' => ['required', 'string', 'size:3'],
            'employee_limit' => ['required', 'integer', 'min:1'],
            'branch_limit' => ['required', 'integer', 'min:1'],
            'device_limit' => ['required', 'integer', 'min:0'],
            'expires_at' => ['nullable', 'date'],
        ]);

        return Tenant::create($data);
    }

    public function show(Tenant $client)
    {
        return $client->load('package');
    }

    public function update(Request $request, Tenant $client)
    {
        $client->update($request->all());

        return $client->fresh('package');
    }

    public function destroy(Tenant $client)
    {
        $client->update(['status' => 'suspended']);

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