<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Repositories\TenantRepository;
use App\Support\Auth;
use App\Support\Request;
use App\Support\Response;

final class TenantsController
{
    public function index(): void
    {
        Auth::requireRole('superadmin');
        Response::json(['data' => (new TenantRepository())->all()]);
    }

    public function store(Request $request): void
    {
        Auth::requireRole('superadmin');
        $tenant = (new TenantRepository())->create($request->input());
        Response::json(['data' => $tenant], 201);
    }

    public function update(Request $request, array $params): void
    {
        Auth::requireRole('superadmin');
        $tenant = (new TenantRepository())->update((int) $params['id'], $request->input());
        Response::json(['data' => $tenant]);
    }

    public function updateOwn(Request $request): void
    {
        $user = Auth::requireRole('tenant_admin');
        $tenant = (new TenantRepository())->updateForTenantAdmin((int) $user['tenant_id'], $request->input());
        Response::json(['data' => $tenant]);
    }
}
