<?php

namespace PloverCRM\Core\Api\V1;

use PloverCRM\Core\Api\AbstractEndpoint;
use PloverCRM\Core\CRM\Schemas\ActivityTypeSchema;
use WP_REST_Request;

/**
 * Activity Types Endpoint
 * 
 * GET /wp-json/plover-crm/v1/activity-types
 */
class ActivityTypesEndpoint extends AbstractEndpoint
{
    public function getRoute(): string
    {
        return '/activity-types';
    }

    public function getMethod(): string
    {
        return 'GET';
    }

    public function handle(WP_REST_Request $request)
    {
        $crm = $this->container->get('crm');

        if (!$crm || !$crm->isAvailable()) {
            return $this->error('CRM not available', 503);
        }

        $crmTypes = $crm->getActivityTypes();
        $crmTypeIds = array_column($crmTypes, 'id');
        
        // Filter out extra types that already exist in CRM types
        $extraTypes = array_filter(
            ActivityTypeSchema::getExtraTypes(),
            fn($type) => !in_array($type['id'], $crmTypeIds)
        );

        return $this->success([
            'types' => $crmTypes,
            'extra_types' => array_values($extraTypes),
        ]);
    }
}
