<?php

namespace App\DTO;

use Carbon\Carbon;

class ReportFilters
{
    public function __construct(
        public ?Carbon $dateFrom = null,
        public ?Carbon $dateTo = null,
        public array $entityTypes = [],        // ['phases','features','inspections','issues','tasks','media']
        public bool $includePhotos = false,
        public string $format = 'html',        // html|excel
        public bool $includeCharts = false,
    ) {}

    public static function fromRequest(array $data): self
    {
        return new self(
            dateFrom: isset($data['date_from']) ? Carbon::parse($data['date_from']) : null,
            dateTo: isset($data['date_to']) ? Carbon::parse($data['date_to']) : null,
            entityTypes: $data['entity_types'] ?? ['phases', 'features', 'inspections', 'issues', 'tasks'],
            includePhotos: (bool) ($data['include_photos'] ?? false),
            format: $data['format'] ?? 'html',
            includeCharts: (bool) ($data['include_charts'] ?? false),
        );
    }

    public function toArray(): array
    {
        return [
            'date_from' => $this->dateFrom?->toDateString(),
            'date_to' => $this->dateTo?->toDateString(),
            'entity_types' => $this->entityTypes,
            'include_photos' => $this->includePhotos,
            'format' => $this->format,
            'include_charts' => $this->includeCharts,
        ];
    }

    public function getAvailableEntities(): array
    {
        return [
            'phases' => 'Fases',
            'features' => 'Elementos (Features)',
            'inspections' => 'Inspecciones',
            'issues' => 'Incidencias (Issues)',
            'tasks' => 'Tareas',
            'media' => 'Archivos/Media',
        ];
    }

    public function getDateRangeLabel(): string
    {
        if ($this->dateFrom && $this->dateTo) {
            return $this->dateFrom->format('d/m/Y').' - '.$this->dateTo->format('d/m/Y');
        }
        if ($this->dateFrom) {
            return 'Desde '.$this->dateFrom->format('d/m/Y');
        }
        if ($this->dateTo) {
            return 'Hasta '.$this->dateTo->format('d/m/Y');
        }

        return 'Todo el período';
    }
}
