<?php

namespace FleekDash\Controllers\SiteGrowth;

use WP_REST_Request;
use WP_REST_Response;

/**
 * Templates Management Trait for SiteGrowthMeterController
 *
 * Handles template-related operations
 *
 * @since 2.2.0 Extracted from SiteGrowthMeterController for better organization
 */
trait TemplatesManagement
{
    /**
     * Get templates
     */
    public function getTemplates(WP_REST_Request $request)
    {
        try {
            $auth = $this->check_auth_graceful('authenticated');

            $category = $request->get_param('category');
            $templates = \FleekDash\Models\SiteGrowthTemplate::getActive($category);

            return new WP_REST_Response([
                'success' => true,
                'templates' => $templates,
                'auth_status' => ($auth['authenticated'] ?? false) ? 'authenticated' : 'fallback'
            ], 200);

        } catch (\Exception $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'Error getting templates: ' . $e->getMessage(),
                'auth_status' => 'error'
            ], 500);
        }
    }

    /**
     * Get single template
     */
    public function getTemplate(WP_REST_Request $request)
    {
        try {
            $auth = $this->check_auth_graceful('authenticated');

            if (!($auth['authenticated'] ?? false)) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Authentication required to get template',
                    'auth_status' => 'fallback'
                ], 401);
            }

            $template_id = $request->get_param('id');
            $template = \FleekDash\Models\SiteGrowthTemplate::find($template_id);

            if (!$template) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Template not found',
                    'auth_status' => 'error'
                ], 404);
            }

            return new WP_REST_Response([
                'success' => true,
                'template' => $template,
                'auth_status' => 'authenticated'
            ], 200);

        } catch (\Exception $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'Error getting template: ' . $e->getMessage(),
                'auth_status' => 'error'
            ], 500);
        }
    }

    /**
     * Apply template
     */
    public function applyTemplate(WP_REST_Request $request)
    {
        try {
            $auth = $this->check_auth_graceful('authenticated');

            if (!($auth['authenticated'] ?? false)) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Authentication required to apply template',
                    'auth_status' => 'fallback'
                ], 401);
            }

            $template_id = $request->get_param('id');
            $template = \FleekDash\Models\SiteGrowthTemplate::find($template_id);

            if (!$template || !is_object($template)) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Template not found',
                    'auth_status' => 'error'
                ], 404);
            }

            $result = is_callable([$template, 'applyToSite']) ? $template->applyToSite() : [];

            return new WP_REST_Response([
                'success' => true,
                'data' => $result,
                'message' => 'Template applied successfully',
                'auth_status' => 'authenticated'
            ], 200);

        } catch (\Exception $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'Error applying template: ' . $e->getMessage(),
                'auth_status' => 'error'
            ], 500);
        }
    }

    /**
     * Get template categories
     */
    public function getCategories(WP_REST_Request $request)
    {
        try {
            $auth = $this->check_auth_graceful('authenticated');

            if (!($auth['authenticated'] ?? false)) {
                return new WP_REST_Response([
                    'success' => false,
                    'message' => 'Authentication required to get categories',
                    'auth_status' => 'fallback'
                ], 401);
            }

            $categories = \FleekDash\Models\SiteGrowthTemplate::getCategories();

            return new WP_REST_Response([
                'success' => true,
                'categories' => $categories,
                'auth_status' => 'authenticated'
            ], 200);

        } catch (\Exception $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'Error getting categories: ' . $e->getMessage(),
                'auth_status' => 'error'
            ], 500);
        }
    }
}
