<?php
/**
 * ServiceTemplatesCatalog · Slice 8Z-w.
 *
 * Read-only reference library of PM / Callout / Report templates. Seeded from
 * sitetracker16's production templates so PMs on a new services project can
 * pick from a proven starter set.
 *
 * Store is metadata-only for now — the full item schema (fields · dependencies)
 * will land when PC/FS use these to build actual visit reports.
 */
class ServiceTemplatesCatalog extends Model
{
    /** All active templates ordered by category then sort_order. */
    public function all(): array
    {
        $db = new DB();
        return $db->mySelect(
            "SELECT * FROM service_templates_catalog
              WHERE is_active = 1
              ORDER BY FIELD(category,
                    'PM_MONTHLY','PM_QUARTERLY','PM_SEMI_ANNUAL','PM_ANNUAL',
                    'PM_ASSET','PM_FUELTEST','CALLOUT','CALLOUT_RESOLUTION','REPORT'
                ), sort_order, slug",
            []
        ) ?: [];
    }

    /**
     * Grouped by category label — used by the Projects/view PM → PC card to
     * render one chip cluster per section.
     */
    public function grouped(): array
    {
        $rows = $this->all();
        $out  = [];
        $labels = [
            'PM_MONTHLY'         => 'Preventive · Monthly',
            'PM_QUARTERLY'       => 'Preventive · Quarterly',
            'PM_SEMI_ANNUAL'     => 'Preventive · Semi-annual',
            'PM_ANNUAL'          => 'Preventive · Annual',
            'PM_ASSET'           => 'Preventive · Asset-specific',
            'PM_FUELTEST'        => 'Preventive · Fuel test',
            'CALLOUT'            => 'Callout',
            'CALLOUT_RESOLUTION' => 'Callout resolution',
            'REPORT'             => 'Report',
        ];
        foreach ($rows as $r) {
            $cat = (string)$r['category'];
            $key = $labels[$cat] ?? $cat;
            if (!isset($out[$key])) $out[$key] = [];
            $out[$key][] = $r;
        }
        return $out;
    }

    /** Item schema (checklist fields) for one template, ordered by section then sort. */
    public function itemsFor(int $templateId): array
    {
        $db = new DB();
        return $db->mySelect(
            "SELECT * FROM service_template_items
              WHERE template_id = :t
              ORDER BY sort_order, c0d",
            [':t' => $templateId]
        ) ?: [];
    }

    /** All callout resolution patterns (18 rows), grouped by category. */
    public function calloutResolutionsGrouped(): array
    {
        $db = new DB();
        $rows = $db->mySelect(
            "SELECT * FROM service_callout_resolutions_catalog
              WHERE is_active = 1
              ORDER BY FIELD(category,'POWER','GEN','BATTERY','COOLING','COMMS','ENVIRONMENT','SECURITY','OTHER'), sort_order",
            []
        ) ?: [];
        $out = [];
        foreach ($rows as $r) {
            $cat = (string)$r['category'];
            if (!isset($out[$cat])) $out[$cat] = [];
            $out[$cat][] = $r;
        }
        return $out;
    }
}
