<?php

namespace App\Http\Controllers;

use App\Models\Client;
use Illuminate\Http\Request;

class ClientController extends Controller
{
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'fb_page' => 'nullable|url'
        ]);

        $clientId = 'CLT_' . rand(1000, 9999);
        $portalKey = \Illuminate\Support\Str::random(32);

        Client::create([
            'client_id' => $clientId,
            'name' => $request->name,
            'phone' => $request->phone,
            'fb_page' => $request->fb_page,
            'status' => 'Active',
            'deposited' => 0,
            'spent' => 0,
            'scopes' => ['Facebook Marketing'],
            'portal_key' => $portalKey
        ]);

        return redirect()->route('dashboard')->with('success', 'Client created successfully!');
    }

    public function addScope(Request $request)
    {
        $request->validate([
            'client_id' => 'required|exists:clients,client_id',
            'scope' => 'required|string'
        ]);

        $client = Client::where('client_id', $request->client_id)->first();
        $scopes = $client->scopes ?? [];
        
        if (!in_array($request->scope, $scopes)) {
            $scopes[] = $request->scope;
            $client->update(['scopes' => $scopes]);
        }

        return response()->json(['success' => true]);
    }

    public function removeScope(Request $request)
    {
        $request->validate([
            'client_id' => 'required|exists:clients,client_id',
            'scope' => 'required|string'
        ]);

        $client = Client::where('client_id', $request->client_id)->first();
        $scopes = array_filter($client->scopes ?? [], function($s) use ($request) {
            return $s !== $request->scope;
        });
        
        $client->update(['scopes' => array_values($scopes)]);

        return response()->json(['success' => true]);
    }

    public function update(Request $request, $id)
    {
        $client = Client::findOrFail($id);
        
        $request->validate([
            'name' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'fb_page' => 'nullable|url',
            'status' => 'required|in:Active,Inactive'
        ]);

        $client->update($request->only(['name', 'phone', 'fb_page', 'status']));
        return redirect()->route('dashboard')->with('success', 'Client updated successfully!');
    }

    public function destroy($id)
    {
        $client = Client::findOrFail($id);
        $client->delete();
        return response()->json(['success' => true]);
    }

    public function getPortalUrl($clientId)
    {
        $client = Client::where('client_id', $clientId)->first();
        if (!$client) {
            return response()->json(['error' => 'Client not found'], 404);
        }

        $portalUrl = "https://portal.example.com/c/{$client->client_id}?key={$client->portal_key}";
        return response()->json(['portal_url' => $portalUrl]);
    }
}
