<?php

namespace App\Http\Controllers\Admin;

use App\Actions\Customers\ExportCustomerGroups;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CustomerGroupRequest;
use App\Models\CustomerGroup;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * Customer groups admin — master-detail picklist (list on the left,
 * sticky editor on the right). AJAX-driven like Categories / Drug
 * Schedules / Adjustment Reasons: saves and deletes flow through XHR,
 * the server returns the freshly-rendered list HTML, and the client
 * swaps it in without a page reload.
 *
 * DESC default sort (newest first) — system-wide convention for tables
 * without a manual `sort_order` column.
 */
class CustomerGroupController extends Controller
{
    public function index(Request $request): View
    {
        $this->authorize('viewAny', CustomerGroup::class);

        $rows       = CustomerGroup::query()->orderByDesc('id')->get();
        $selectedId = $request->integer('selected');
        $isNew      = $request->boolean('new');
        $selected   = $selectedId ? $rows->firstWhere('id', $selectedId) : null;

        return view('admin.customer-groups.index', [
            'rows'     => $rows,
            'selected' => $selected,
            'isNew'    => $isNew,
        ]);
    }

    public function store(CustomerGroupRequest $request): RedirectResponse|JsonResponse
    {
        $this->authorize('create', CustomerGroup::class);

        $row     = CustomerGroup::create($request->persistedAttributes());
        $message = __('customers.groups.flash.created', ['name' => $row->name]);

        if ($request->wantsJson()) {
            return $this->freshListJson($row->id, $message);
        }

        return redirect()
            ->route('admin.customer-groups.index', ['selected' => $row->id])
            ->with('success', $message);
    }

    public function update(CustomerGroupRequest $request, CustomerGroup $customerGroup): RedirectResponse|JsonResponse
    {
        $this->authorize('update', $customerGroup);

        $customerGroup->update($request->persistedAttributes());
        $message = __('customers.groups.flash.updated', ['name' => $customerGroup->name]);

        if ($request->wantsJson()) {
            return $this->freshListJson($customerGroup->id, $message);
        }

        return redirect()
            ->route('admin.customer-groups.index', ['selected' => $customerGroup->id])
            ->with('success', $message);
    }

    public function destroy(Request $request, CustomerGroup $customerGroup): RedirectResponse|JsonResponse
    {
        $this->authorize('delete', $customerGroup);

        $name = $customerGroup->name;

        if ($customerGroup->customers()->exists()) {
            $msg = __('customers.groups.errors.in_use', ['name' => $name]);
            if ($request->wantsJson()) {
                return response()->json(['ok' => false, 'message' => $msg], 422);
            }
            return redirect()
                ->route('admin.customer-groups.index', ['selected' => $customerGroup->id])
                ->with('error', $msg);
        }

        $customerGroup->delete();
        $message = __('customers.groups.flash.deleted', ['name' => $name]);

        if ($request->wantsJson()) {
            return $this->freshListJson(null, $message);
        }

        return redirect()
            ->route('admin.customer-groups.index')
            ->with('success', $message);
    }

    /**
     * Server-rendered list HTML + a structured row snapshot for the
     * Alpine `rowsById` lookup. Returned after every successful save /
     * delete / toggle so the client can swap in one go without a reload.
     */

    public function export(Request $request, ExportCustomerGroups $export): \Symfony\Component\HttpFoundation\StreamedResponse
    {
        $this->authorize('viewAny', CustomerGroup::class);

        $format = $request->query('format', 'csv');
        if (! in_array($format, ['csv', 'xlsx'], true)) {
            abort(422, 'Unsupported export format. Use csv or xlsx.');
        }

        return ($export)($format);
    }

    private function freshListJson(?int $selectedId, string $message): JsonResponse
    {
        $rows = CustomerGroup::query()->orderByDesc('id')->get();

        $listHtml = view('admin.customer-groups._list', ['rows' => $rows])->render();

        $rowsForJs = $rows->map(fn (CustomerGroup $r) => [
            'id'                       => $r->id,
            'name'                     => $r->name,
            'default_discount_percent' => $r->default_discount_percent !== null ? (float) $r->default_discount_percent : null,
            'is_active'                => (bool) $r->is_active,
            'updated_at'               => $r->updated_at?->diffForHumans(),
        ])->values();

        return response()->json([
            'ok'        => true,
            'message'   => $message,
            'id'        => $selectedId,
            'list_html' => $listHtml,
            'rows'      => $rowsForJs,
        ]);
    }
}
