<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ActivityLog;
use App\Models\ResellerPlan;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class ResellerPlanController extends Controller
{
    /**
     * Afișează lista de pachete reseller.
     */
    public function index()
    {
        $resellerPlans = ResellerPlan::withCount('resellers')->get();
        return view('admin.reseller-plans.index', compact('resellerPlans'));
    }

    /**
     * Afișează formularul pentru crearea unui pachet reseller nou.
     */
    public function create()
    {
        return view('admin.reseller-plans.create');
    }

    /**
     * Salvează un pachet reseller nou în baza de date.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'price' => ['required', 'numeric', 'min:0'],
            'max_clients' => ['required', 'integer', 'min:1'],
            'dns_zones_per_client' => ['required', 'integer', 'min:1'],
            'subdomains_per_client' => ['required', 'integer', 'min:1'],
            'nameservers_per_client' => ['required', 'integer', 'min:1'],
            'allowed_record_types' => ['nullable', 'array'],
            'allowed_templates' => ['nullable', 'array'],
            'is_active' => ['boolean'],
            'description' => ['nullable', 'string'],
        ]);

        $resellerPlan = ResellerPlan::create([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'price' => $request->price,
            'max_clients' => $request->max_clients,
            'dns_zones_per_client' => $request->dns_zones_per_client,
            'subdomains_per_client' => $request->subdomains_per_client,
            'nameservers_per_client' => $request->nameservers_per_client,
            'allowed_record_types' => $request->allowed_record_types ?? [],
            'allowed_nameservers' => $request->allowed_nameservers ?? [],
            'allowed_templates' => $request->allowed_templates ?? [],
            'is_active' => $request->boolean('is_active', true),
            'description' => $request->description,
        ]);

        ActivityLog::log('create', "A creat pachetul reseller: {$resellerPlan->name}", 'ResellerPlan', $resellerPlan->id);

        return redirect()->route('admin.reseller-plans.index')
            ->with('success', 'Pachetul reseller a fost creat cu succes.');
    }

    /**
     * Afișează detaliile unui pachet reseller specific.
     */
    public function show(ResellerPlan $resellerPlan)
    {
        $resellerPlan->load('resellers.clients');
        return view('admin.reseller-plans.show', compact('resellerPlan'));
    }

    /**
     * Afișează formularul pentru editarea unui pachet reseller.
     */
    public function edit(ResellerPlan $resellerPlan)
    {
        return view('admin.reseller-plans.edit', compact('resellerPlan'));
    }

    /**
     * Actualizează datele unui pachet reseller în baza de date.
     */
    public function update(Request $request, ResellerPlan $resellerPlan)
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'price' => ['required', 'numeric', 'min:0'],
            'max_clients' => ['required', 'integer', 'min:1'],
            'dns_zones_per_client' => ['required', 'integer', 'min:1'],
            'subdomains_per_client' => ['required', 'integer', 'min:1'],
            'nameservers_per_client' => ['required', 'integer', 'min:1'],
            'allowed_record_types' => ['nullable', 'array'],
            'allowed_templates' => ['nullable', 'array'],
            'is_active' => ['boolean'],
            'description' => ['nullable', 'string'],
        ]);

        $oldData = $resellerPlan->toArray();

        $resellerPlan->update([
            'name' => $request->name,
            'slug' => Str::slug($request->name),
            'price' => $request->price,
            'max_clients' => $request->max_clients,
            'dns_zones_per_client' => $request->dns_zones_per_client,
            'subdomains_per_client' => $request->subdomains_per_client,
            'nameservers_per_client' => $request->nameservers_per_client,
            'allowed_record_types' => $request->allowed_record_types ?? [],
            'allowed_nameservers' => $request->allowed_nameservers ?? [],
            'allowed_templates' => $request->allowed_templates ?? [],
            'is_active' => $request->boolean('is_active'),
            'description' => $request->description,
        ]);

        ActivityLog::log('update', "A actualizat pachetul reseller: {$resellerPlan->name}", 'ResellerPlan', $resellerPlan->id, [
            'old' => $oldData,
            'new' => $resellerPlan->fresh()->toArray()
        ]);

        return redirect()->route('admin.reseller-plans.index')
            ->with('success', 'Pachetul reseller a fost actualizat cu succes.');
    }

    /**
     * Șterge un pachet reseller din baza de date.
     */
    public function destroy(ResellerPlan $resellerPlan)
    {
        if ($resellerPlan->resellers()->count() > 0) {
            return redirect()->route('admin.reseller-plans.index')
                ->with('error', 'Nu poți șterge acest pachet deoarece are reselleri asociați.');
        }

        $planName = $resellerPlan->name;
        $resellerPlan->delete();

        ActivityLog::log('delete', "A șters pachetul reseller: {$planName}", 'ResellerPlan', $resellerPlan->id);

        return redirect()->route('admin.reseller-plans.index')
            ->with('success', 'Pachetul reseller a fost șters cu succes.');
    }
}
