<?php

namespace PloverCRM\Core\Api\V1;

use PloverCRM\Core\Api\AbstractEndpoint;
use WP_REST_Request;

/**
 * Search Endpoint
 * 
 * Advanced search for contacts with filters
 * 
 * GET /wp-json/plover-crm/v1/search
 * 
 * Parameters:
 * - query: Search term
 * - page: Page number (default: 1)
 * - per_page: Items per page (default: 20, max: 100)
 * - lists: Comma-separated list IDs (OR within lists)
 * - tags: Comma-separated tag IDs (OR within tags)
 * - stages: Comma-separated stage names (exact match on stage custom field)
 * - fields: Comma-separated field names to search in
 * - custom_fields: Comma-separated custom field slugs to search in
 * - search_mode: 'or' or 'and' for field matching (default: 'or')
 */
class SearchEndpoint extends AbstractEndpoint
{
    public function getRoute(): string
    {
        return '/search';
    }

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

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

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

        $query = sanitize_text_field($request->get_param('query') ?? '');
        $page = max(1, intval($request->get_param('page') ?? 1));
        $per_page = min(100, max(1, intval($request->get_param('per_page') ?? 20)));
        $lists = sanitize_text_field($request->get_param('lists') ?? '');
        $tags = sanitize_text_field($request->get_param('tags') ?? '');
        $fields = sanitize_text_field($request->get_param('fields') ?? '');
        $custom_fields = sanitize_text_field($request->get_param('custom_fields') ?? '');
        $statuses = sanitize_text_field($request->get_param('statuses') ?? '');
        $stages = sanitize_text_field($request->get_param('stages') ?? '');
        $search_mode = $request->get_param('search_mode') ?? 'or';

        // Parse comma-separated values
        $list_ids = !empty($lists) ? array_map('intval', explode(',', $lists)) : [];
        $tag_ids = !empty($tags) ? array_map('intval', explode(',', $tags)) : [];
        $field_names = !empty($fields) ? array_map('trim', explode(',', $fields)) : [];
        $custom_field_slugs = !empty($custom_fields) ? array_map('trim', explode(',', $custom_fields)) : [];
        $status_list = !empty($statuses) ? array_map('trim', explode(',', $statuses)) : [];
        $stage_names = !empty($stages) ? array_map('trim', explode(',', $stages)) : [];

        // Use provider's searchContacts method
        $result = $crm->searchContacts([
            'query' => $query,
            'page' => $page,
            'per_page' => $per_page,
            'list_ids' => $list_ids,
            'tag_ids' => $tag_ids,
            'fields' => $field_names,
            'custom_fields' => $custom_field_slugs,
            'search_mode' => $search_mode,
            'statuses' => $status_list,
            'stages' => $stage_names,
        ]);

        return $this->success($result['contacts'], [
            'total' => $result['total'],
            'page' => $page,
            'per_page' => $per_page,
            'total_pages' => $result['total_pages'],
        ]);
    }
}
