<?php

namespace App\Actions\Dashboard;

use App\Models\Category;
use App\Models\Customer;
use App\Models\PaymentMethod;
use App\Models\Product;
use App\Models\TaxGroup;
use App\Models\Terminal;

/**
 * Builds the dashboard "Get your store ready" checklist — the handful of
 * things a brand-new install needs before it can ring up real sales. Each
 * step is a single cheap existence check against the relevant table and
 * links to the page where the owner completes it.
 *
 * Pure read; returns a plain array the dashboard blade renders. The card
 * auto-hides once `complete` is true (see DashboardController::index).
 */
class BuildSetupChecklist
{
    /**
     * @return array{
     *     steps: array<int, array{key:string,label:string,description:string,url:string,done:bool}>,
     *     done: int, total: int, percent: int, complete: bool
     * }
     */
    public function __invoke(): array
    {
        $steps = [
            [
                'key'         => 'category',
                'label'       => __('admin.dashboard.setup.steps.category.label'),
                'description' => __('admin.dashboard.setup.steps.category.desc'),
                'url'         => route('admin.categories.index'),
                'done'        => Category::query()->exists(),
            ],
            [
                'key'         => 'product',
                'label'       => __('admin.dashboard.setup.steps.product.label'),
                'description' => __('admin.dashboard.setup.steps.product.desc'),
                'url'         => route('admin.products.create'),
                'done'        => Product::query()->exists(),
            ],
            [
                'key'         => 'payment_method',
                'label'       => __('admin.dashboard.setup.steps.payment_method.label'),
                'description' => __('admin.dashboard.setup.steps.payment_method.desc'),
                'url'         => route('admin.settings.payment-methods.edit'),
                'done'        => PaymentMethod::query()->active()->exists(),
            ],
            [
                'key'         => 'tax',
                'label'       => __('admin.dashboard.setup.steps.tax.label'),
                'description' => __('admin.dashboard.setup.steps.tax.desc'),
                'url'         => route('admin.settings.tax.groups.index'),
                'done'        => TaxGroup::query()->exists(),
            ],
            [
                'key'         => 'customer',
                'label'       => __('admin.dashboard.setup.steps.customer.label'),
                'description' => __('admin.dashboard.setup.steps.customer.desc'),
                'url'         => route('admin.customers.create'),
                'done'        => Customer::query()->exists(),
            ],
            [
                'key'         => 'terminal',
                'label'       => __('admin.dashboard.setup.steps.terminal.label'),
                'description' => __('admin.dashboard.setup.steps.terminal.desc'),
                'url'         => route('admin.terminals.index'),
                'done'        => Terminal::query()->exists(),
            ],
        ];

        $total = count($steps);
        $done  = count(array_filter($steps, fn ($s) => $s['done']));

        return [
            'steps'    => $steps,
            'done'     => $done,
            'total'    => $total,
            'percent'  => $total > 0 ? (int) round($done / $total * 100) : 100,
            'complete' => $done === $total,
        ];
    }
}
