<?php
/**
 * ITSWEBER CMS – Property Cards Section (Estate Theme)
 *
 * Displays property listing cards with image, price, features and link.
 * Theme-bound: only available when the Estate theme is active.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

namespace App\Sections\Theme\Estate;

use App\Sections\AbstractSection;
use App\Core\Request;

class PropertyCardsSection extends AbstractSection
{
    public static function typeKey(): string    { return 'property_cards'; }
    public static function label(): string      { return 'Property Cards'; }
    public static function category(): string   { return 'content'; }
    public static function themeSlug(): ?string { return 'estate'; }
    public static function description(): string { return 'Immobilien-Karten mit Bild, Preis und Ausstattung'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>'
             . '<path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $titles   = $request->input('property_title', []);
        $prices   = $request->input('property_price', []);
        $images   = $request->input('property_image', []);
        $urls     = $request->input('property_url', []);
        $badges   = $request->input('property_badge', []);
        $features = $request->input('property_features', []);

        $items = [];
        if (is_array($titles)) {
            foreach ($titles as $i => $title) {
                $t = trim((string) $title);
                if ($t === '') continue;

                $items[] = [
                    'title'    => $t,
                    'price'    => trim((string) ($prices[$i] ?? '')),
                    'image'    => trim((string) ($images[$i] ?? '')),
                    'url'      => trim((string) ($urls[$i] ?? '')),
                    'badge'    => trim((string) ($badges[$i] ?? '')),
                    'features' => trim((string) ($features[$i] ?? '')),
                ];
            }
        }
        $items = array_slice($items, 0, 6);

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
            'cta_text'           => trim((string) ($request->input('cta_text', '') ?? '')),
            'cta_url'            => trim((string) ($request->input('cta_url', '') ?? '')),
            'items'              => $items,
        ];

        return $this->mergeThemeExtras($data, $request);
    }

    public function defaultData(): array
    {
        return [
            'title'    => 'Ausgewaehlte Immobilien',
            'subtitle' => 'Entdecken Sie unsere aktuellen Top-Objekte – handverlesen fuer hoechste Ansprueche.',
            'color_scheme' => '', 'section_text_color' => '',
            'cta_text' => 'Alle Objekte ansehen',
            'cta_url'  => '/ueber-uns',
            'items'    => [
                ['title' => 'Penthouse Rheinblick',   'price' => '1.250.000 EUR', 'image' => '', 'url' => '', 'badge' => 'Exklusiv', 'features' => '4 Zimmer, 185 m², Dachterrasse'],
                ['title' => 'Stadtvilla am Park',     'price' => '890.000 EUR',   'image' => '', 'url' => '', 'badge' => 'Neu',      'features' => '5 Zimmer, 220 m², Garten'],
                ['title' => 'Loft in der Altstadt',    'price' => '425.000 EUR',   'image' => '', 'url' => '', 'badge' => '',          'features' => '2 Zimmer, 95 m², Balkon'],
            ],
        ];
    }
}
