<?php
/**
 * Custom Columns for `proto_project` WP_List_Table
 */

if (!defined('ABSPATH')) exit;

class Proto_Preview_Columns {

    public function init() {
        add_filter('manage_proto_project_posts_columns', [$this, 'set_custom_columns']);
        add_action('manage_proto_project_posts_custom_column', [$this, 'render_custom_columns'], 10, 2);
        add_filter('post_row_actions', [$this, 'add_player_row_actions'], 10, 2);
    }

    public function set_custom_columns($columns) {
        $new_columns = [];
        $new_columns['cb'] = $columns['cb'];
        $new_columns['title'] = __('Project Name', 'proto-preview');
        $new_columns['client_name'] = __('Client', 'proto-preview');
        $new_columns['screens_count'] = __('Screens & Variations', 'proto-preview');
        $new_columns['stakeholders'] = __('Stakeholders', 'proto-preview');
        $new_columns['player_action'] = __('InVision Player', 'proto-preview');
        $new_columns['date'] = $columns['date'];
        return $new_columns;
    }

    public function render_custom_columns($column, $post_id) {
        if ($column === 'client_name') {
            $client_name = get_post_meta($post_id, '_proto_client_name', true);
            if (!empty($client_name)) {
                echo '<strong style="color:#1e293b;">' . esc_html($client_name) . '</strong>';
            } else {
                $client_terms = wp_get_post_terms($post_id, 'proto_client', ['fields' => 'names']);
                if (!empty($client_terms)) {
                    echo '<strong style="color:#1e293b;">' . esc_html($client_terms[0]) . '</strong>';
                } else {
                    echo '<span style="color:#94a3b8; font-style:italic;">—</span>';
                }
            }
        }

        if ($column === 'screens_count') {
            $screens = get_post_meta($post_id, '_proto_screens', true);
            if (is_array($screens)) {
                $total_screens = count($screens);
                $total_vars = 0;
                foreach ($screens as $s) {
                    $total_vars += isset($s['layouts']) ? count($s['layouts']) : 0;
                }
                echo '<strong>' . esc_html($total_screens) . '</strong> Screen' . ($total_screens > 1 ? 's' : '') . ' <span style="color:#94a3b8;">(' . esc_html($total_vars) . ' Variation' . ($total_vars > 1 ? 's' : '') . ')</span>';
            } else {
                echo '<span style="color: #94a3b8;">1 Screen</span>';
            }
        }

        if ($column === 'stakeholders') {
            $assigned_user_ids = get_post_meta($post_id, '_proto_assigned_users', true);
            $user_project_roles = get_post_meta($post_id, '_proto_user_project_roles', true) ?: [];
            $extra_emails = get_post_meta($post_id, '_proto_notification_emails', true);
            $names = [];

            if (is_array($assigned_user_ids) && !empty($assigned_user_ids)) {
                foreach ($assigned_user_ids as $u_id) {
                    $u = get_user_by('id', (int)$u_id);
                    if ($u) {
                        $role = $user_project_roles[$u->ID] ?? ($u->ID == 1 ? 'Designer' : ($u->user_email === 'rian@rian.ca' ? 'Project Manager' : 'Team'));
                        $names[] = '<span style="font-weight:600; color:#1e293b;">' . esc_html($u->display_name) . '</span> <span style="font-size:10px; color:#64748b;">(' . esc_html($role) . ')</span>';
                    }
                }
            }

            if (!empty($names)) {
                echo implode(', ', $names);
            } else {
                echo '<span style="color:#94a3b8; font-size:11px;">Site Admin</span>';
            }

            if (!empty($extra_emails)) {
                echo '<br><span style="font-size:10px; color:#64748b;">✉️ ' . esc_html($extra_emails) . '</span>';
            }
        }

        if ($column === 'player_action') {
            $player_url = home_url('/?proto_preview=1&project_id=' . $post_id);
            echo '<a href="' . esc_url($player_url) . '" target="_blank" class="button button-small" style="background:#b02b2b; color:#fff; border-color:#872e21; font-weight:bold;">'
                 . '🚀 Open Player ↗'
                 . '</a>';
        }
    }

    public function add_player_row_actions($actions, $post) {
        if ($post->post_type === 'proto_project') {
            $view_url = get_permalink($post->ID);
            $player_url = home_url('/?proto_preview=1&project_id=' . $post->ID);
            $actions['view'] = '<a href="' . esc_url($view_url) . '" target="_blank" rel="noopener" style="color:#0284c7; font-weight:600;">' . __('View Overview ↗', 'proto-preview') . '</a>';
            $actions['open_player'] = '<a href="' . esc_url($player_url) . '" target="_blank" style="color:#b02b2b; font-weight:bold;">' . __('Open in Player ↗', 'proto-preview') . '</a>';
        }
        return $actions;
    }
}
