<?php

declare(strict_types=1);

namespace App\Repositories;

final class PetitionRepository extends BaseRepository
{
    public function active(): array
    {
        return [
            ['title' => 'Безопасни тротоари в квартала', 'supporters' => 842, 'goal' => 1200, 'status' => 'Активна'],
            ['title' => 'Повече контейнери за разделно събиране', 'supporters' => 431, 'goal' => 800, 'status' => 'Активна'],
            ['title' => 'Осветление около училището', 'supporters' => 690, 'goal' => 700, 'status' => 'Изпратена'],
        ];
    }

    public function create(array $data): array
    {
        if ($this->pdo) {
            $statement = $this->pdo->prepare(
                'INSERT INTO petitions (user_id, title, description, goals, status, created_at)
                 VALUES (:user_id, :title, :description, :goals, :status, NOW())'
            );
            $statement->execute([
                'user_id' => $data['user_id'] ?? null,
                'title' => $data['title'],
                'description' => $data['description'],
                'goals' => $data['goals'],
                'status' => 'active',
            ]);
        }

        return [
            'id' => $this->pdo ? (int) $this->pdo->lastInsertId() : random_int(100, 999),
            'supporters' => 1,
            'goal' => (int) ($data['goal'] ?? 500),
            'status' => 'Активна',
            ...$data,
        ];
    }
}
