<?php

namespace App\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use App\Models\Business;
use App\Models\Plan;
use App\Services\SuperAdmin\PlatformAdminService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\View\View;

class BusinessController extends Controller
{
    public function index(Request $request, PlatformAdminService $platform): View
    {
        Gate::authorize('viewAny', Business::class);

        return view('super-admin.businesses.index', [
            'businesses' => $platform->businesses($request),
            'plans' => Plan::query()->orderBy('sort_order')->get(),
            'filters' => $request->only(['q', 'plan_id', 'status']),
        ]);
    }

    public function show(Business $business, PlatformAdminService $platform): View
    {
        Gate::authorize('view', $business);

        return view('super-admin.businesses.show', $platform->businessDetails($business));
    }

    public function suspend(Request $request, Business $business, PlatformAdminService $platform): RedirectResponse
    {
        Gate::authorize('suspend', $business);
        $platform->suspendBusiness($business, $request->user(), $request);

        return redirect()->route('super-admin.businesses.show', $business)->with('status', 'Business suspended successfully.');
    }

    public function activate(Request $request, Business $business, PlatformAdminService $platform): RedirectResponse
    {
        Gate::authorize('activate', $business);
        $platform->activateBusiness($business, $request->user(), $request);

        return redirect()->route('super-admin.businesses.show', $business)->with('status', 'Business activated successfully.');
    }

    public function loginAsOwner(Request $request, Business $business, PlatformAdminService $platform): RedirectResponse
    {
        Gate::authorize('loginAsOwner', $business);
        $platform->log($request->user(), 'business.login_as_owner.placeholder', "Requested login-as-owner placeholder for {$business->name}.", $business, [], $request);

        return redirect()->route('super-admin.businesses.show', $business)
            ->with('status', 'Login as business owner is a placeholder. Real impersonation will be implemented later.');
    }
}
