<?php

namespace App\PageBuilder\Components\Dynamic;

use App\PageBuilder\PageComponent;
use App\Models\Announcement;

class LatestAnnouncementsComponent extends PageComponent
{
    public function key(): string { return 'latest_announcements'; }
    public function category(): string { return 'dynamic'; }
    public function label(): string { return 'Latest Announcements'; }
    public function icon(): string { return 'fas fa-bullhorn'; }

    public function schema(): array
    {
        return [
            'title' => ['type' => 'text', 'default' => 'Son Duyurular'],
            'subtitle' => ['type' => 'textarea', 'label' => 'Alt başlık'],
            'count' => ['type' => 'number', 'label' => 'Adet', 'min' => 1, 'max' => 10, 'default' => 3],
            'show_date' => ['type' => 'boolean', 'label' => 'Tarih göster', 'default' => true],
            'show_excerpt' => ['type' => 'boolean', 'label' => 'Özet göster', 'default' => true],
            'show_image' => ['type' => 'boolean', 'label' => 'Görsel göster', 'default' => false],
            'cta_text' => ['type' => 'text', 'label' => 'Tümünü gör butonu metni'],
            'cta_link' => ['type' => 'link', 'label' => 'Tümünü gör URL'],
            'options' => [
                'columns' => ['type' => 'select', 'options' => [1,2,3], 'default' => 3],
                'color' => ['type' => 'color_token', 'default' => 'transparent'],
                'padding' => ['type' => 'padding_token', 'default' => 'lg'],
                'card_style' => ['type' => 'select', 'options' => ['flat', 'bordered', 'shadow'], 'default' => 'bordered'],
            ],
        ];
    }

    public function defaults(): array
    {
        return [
            'title' => 'Son Duyurular',
            'subtitle' => null,
            'count' => 3,
            'show_date' => true,
            'show_excerpt' => true,
            'show_image' => false,
            'cta_text' => null,
            'cta_link' => null,
            'options' => ['columns' => 3, 'color' => 'transparent', 'padding' => 'lg', 'card_style' => 'bordered'],
        ];
    }

    public function render(array $data, array $options): string
    {
        $count = max(1, min(10, (int) ($data['count'] ?? 3)));
        $announcements = Announcement::where('status', 'published')
            ->orderByDesc('published_at')
            ->limit($count)
            ->get();

        return view('page-builder.components.latest_announcements', compact('data', 'options', 'announcements'))->render();
    }
}
