<?php
/**
 * View Context Helper
 * Provides context information for views
 *
 * @package Freelance_Studio
 */

class FS_View_Context {

    /**
     * Check if we're in admin context
     */
    public static function is_admin() {
        return is_admin();
    }

    /**
     * Check if we're in frontend context
     */
    public static function is_frontend() {
        return !is_admin();
    }

    /**
     * Get current page
     */
    public static function get_current_page() {
        return isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
    }

    /**
     * Get current action
     */
    public static function get_current_action() {
        return isset($_GET['action']) ? sanitize_text_field($_GET['action']) : 'list';
    }

    /**
     * Format currency
     */
    public static function format_currency($amount) {
        return '$' . number_format(floatval($amount), 2);
    }

    /**
     * Format date
     */
    public static function format_date($date, $format = null) {
        if (empty($date)) {
            return '';
        }
        
        if ($format === null) {
            $format = get_option('date_format', 'Y-m-d');
        }
        
        return gmdate($format, strtotime($date));
    }

    /**
     * Get status badge HTML
     */
    public static function status_badge($status, $type = 'default') {
        $classes = array(
            'pending' => 'status-pending',
            'active' => 'status-active',
            'completed' => 'status-completed',
            'cancelled' => 'status-cancelled',
            'on_hold' => 'status-on-hold',
            'draft' => 'status-draft',
            'sent' => 'status-sent',
            'viewed' => 'status-viewed',
            'paid' => 'status-paid',
            'partial' => 'status-partial',
            'overdue' => 'status-overdue',
            'approved' => 'status-approved',
            'rejected' => 'status-rejected',
            'dismissed' => 'status-dismissed',
            'revision_requested' => 'status-revision',
            'revised' => 'status-revised',
            'expired' => 'status-expired',
            'converted' => 'status-converted',
        );
        
        $class = isset($classes[$status]) ? $classes[$status] : 'status-default';
        $label = ucwords(str_replace('_', ' ', $status));
        
        return sprintf('<span class="fs-status-badge %s">%s</span>', esc_attr($class), esc_html($label));
    }

    /**
     * Get priority badge HTML
     */
    public static function priority_badge($priority) {
        $colors = array(
            'low' => '#6b7280',
            'medium' => '#f59e0b',
            'high' => '#f97316',
            'urgent' => '#ef4444',
        );
        
        $color = isset($colors[$priority]) ? $colors[$priority] : '#6b7280';
        $label = ucfirst($priority);
        
        return sprintf(
            '<span class="fs-priority-badge" style="color: %s;">%s</span>',
            esc_attr($color),
            esc_html($label)
        );
    }

    /**
     * Display admin notice
     */
    public static function admin_notice($message, $type = 'success') {
        $class = 'notice notice-' . $type . ' is-dismissible';
        printf('<div class="%s"><p>%s</p></div>', esc_attr($class), esc_html($message));
    }

    /**
     * Get task status labels, respecting custom kanban column names.
     *
     * @param string|null $status  Optional single status key to get label for.
     * @return array|string        Full map, or single label if $status provided.
     */
    public static function get_task_status_labels($status = null) {
        static $labels = null;

        if ($labels === null) {
            $saved = get_option('fs_kanban_column_labels', array());
            $labels = array(
                'requested'   => __('Requested', 'freelance-studio'),
                'pending'     => isset($saved['pending'])     ? $saved['pending']     : __('To Do', 'freelance-studio'),
                'in_progress' => isset($saved['in_progress']) ? $saved['in_progress'] : __('In Progress', 'freelance-studio'),
                'needs_info'  => isset($saved['needs_info'])  ? $saved['needs_info']  : __('Needs Information', 'freelance-studio'),
                'with_client' => isset($saved['with_client']) ? $saved['with_client'] : __('With Client', 'freelance-studio'),
                'completed'   => isset($saved['completed'])   ? $saved['completed']   : __('Done', 'freelance-studio'),
                'cancelled'   => __('Cancelled', 'freelance-studio'),
                'archived'    => __('Archived', 'freelance-studio'),
            );
        }

        if ($status !== null) {
            return $labels[$status] ?? ucfirst(str_replace('_', ' ', $status));
        }

        return $labels;
    }

    /**
     * Get task status colors.
     */
    public static function get_task_status_colors() {
        return array(
            'requested'   => '#f59e0b',
            'pending'     => '#6b7280',
            'in_progress' => '#3b82f6',
            'needs_info'  => '#f59e0b',
            'with_client' => '#8b5cf6',
            'completed'   => '#22c55e',
            'cancelled'   => '#ef4444',
            'archived'    => '#9ca3af',
        );
    }

    /**
     * Output CSS for custom activity badge colors.
     * Uses FS palette colors assigned deterministically by hashing the activity key.
     * Call once per page that displays activity badges.
     */
    public static function render_custom_activity_css() {
        static $rendered = false;
        if ($rendered) return;
        $rendered = true;

        $default_keys = array('development', 'design', 'meeting', 'research', 'admin', 'other');
        $palette = array(
            array('bg' => '#EDE9F5', 'color' => '#5D4E80'),  // purple
            array('bg' => '#FFE0CE', 'color' => '#C65D45'),  // coral
            array('bg' => '#EBF2C7', 'color' => '#5A6B1A'),  // lime
            array('bg' => '#E0F7F3', 'color' => '#2BA891'),  // teal
            array('bg' => '#FCE4EC', 'color' => '#AD1457'),  // pink
            array('bg' => '#E3F2FD', 'color' => '#1565C0'),  // blue
            array('bg' => '#FFF8E1', 'color' => '#F57F17'),  // amber
            array('bg' => '#F3E5F5', 'color' => '#7B1FA2'),  // deep purple
        );

        $custom_activities = get_option('fs_custom_activities', array());
        if (!is_array($custom_activities) || empty($custom_activities)) return;

        $css = '<style>';
        foreach ($custom_activities as $activity) {
            $key = sanitize_key($activity);
            if (empty($key) || in_array($key, $default_keys)) continue;
            $index = abs(crc32($key)) % count($palette);
            $p = $palette[$index];
            $css .= '.fs-activity-' . esc_attr($key) . '{background:' . $p['bg'] . ';color:' . $p['color'] . ';}';
        }
        $css .= '</style>';
        echo $css;    }
}
