<?php

declare(strict_types=1);

namespace App\Framework\Admin\ValueObjects;

/**
 * CRUD Configuration Value Object
 *
 * Configuration for admin CRUD operations
 */
final readonly class CrudConfig
{
    /**
     * @param string $resource Resource name (e.g., 'campaigns', 'users')
     * @param string $resourceName Singular display name (e.g., 'Campaign', 'User')
     * @param string $title Page title
     * @param array<string, mixed> $columns Column definitions for index view
     * @param bool $canCreate Can create new items
     * @param bool $canEdit Can edit items
     * @param bool $canView Can view items
     * @param bool $canDelete Can delete items
     * @param array<string, mixed>|null $filters Filter definitions
     * @param array<string, mixed>|null $bulkActions Bulk action definitions
     * @param bool $searchable Enable search
     */
    public function __construct(
        public string $resource,
        public string $resourceName,
        public string $title,
        public array $columns = [],
        public bool $canCreate = true,
        public bool $canEdit = true,
        public bool $canView = true,
        public bool $canDelete = true,
        public ?array $filters = null,
        public ?array $bulkActions = null,
        public bool $searchable = true,
    ) {
    }

    public static function forResource(
        string $resource,
        string $resourceName,
        string $title
    ): self {
        return new self(
            resource: $resource,
            resourceName: $resourceName,
            title: $title
        );
    }

    public function withColumns(array $columns): self
    {
        return new self(
            resource: $this->resource,
            resourceName: $this->resourceName,
            title: $this->title,
            columns: $columns,
            canCreate: $this->canCreate,
            canEdit: $this->canEdit,
            canView: $this->canView,
            canDelete: $this->canDelete,
            filters: $this->filters,
            bulkActions: $this->bulkActions,
            searchable: $this->searchable
        );
    }

    public function withPermissions(
        bool $canCreate = true,
        bool $canEdit = true,
        bool $canView = true,
        bool $canDelete = true
    ): self {
        return new self(
            resource: $this->resource,
            resourceName: $this->resourceName,
            title: $this->title,
            columns: $this->columns,
            canCreate: $canCreate,
            canEdit: $canEdit,
            canView: $canView,
            canDelete: $canDelete,
            filters: $this->filters,
            bulkActions: $this->bulkActions,
            searchable: $this->searchable
        );
    }

    public function withFilters(array $filters): self
    {
        return new self(
            resource: $this->resource,
            resourceName: $this->resourceName,
            title: $this->title,
            columns: $this->columns,
            canCreate: $this->canCreate,
            canEdit: $this->canEdit,
            canView: $this->canView,
            canDelete: $this->canDelete,
            filters: $filters,
            bulkActions: $this->bulkActions,
            searchable: $this->searchable
        );
    }

    public function withBulkActions(array $bulkActions): self
    {
        return new self(
            resource: $this->resource,
            resourceName: $this->resourceName,
            title: $this->title,
            columns: $this->columns,
            canCreate: $this->canCreate,
            canEdit: $this->canEdit,
            canView: $this->canView,
            canDelete: $this->canDelete,
            filters: $this->filters,
            bulkActions: $bulkActions,
            searchable: $this->searchable
        );
    }

    public function toArray(): array
    {
        return [
            'resource' => $this->resource,
            'resourceName' => $this->resourceName,
            'title' => $this->title,
            'columns' => $this->columns,
            'canCreate' => $this->canCreate,
            'canEdit' => $this->canEdit,
            'canView' => $this->canView,
            'canDelete' => $this->canDelete,
            'hasActions' => $this->canEdit || $this->canView || $this->canDelete,
            'filters' => $this->filters,
            'bulkActions' => $this->bulkActions,
            'searchable' => $this->searchable,
        ];
    }
}
