<?php
/**
 * Time Tracking Reports View
 *
 * @package Freelance_Studio
 */

if (!defined('ABSPATH')) {
    exit;
}
?>

<div class="wrap fs-wrap">
    <h1><?php esc_html_e('Time Tracking', 'freelance-studio'); ?></h1>
    
    <!-- Tab Navigation -->
    <div class="fs-tab-nav">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking') ); ?>" class="fs-tab"><?php esc_html_e('Timer view', 'freelance-studio'); ?></a>
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=week') ); ?>" class="fs-tab"><?php esc_html_e('Week view', 'freelance-studio'); ?></a>
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking&action=reports') ); ?>" class="fs-tab active"><?php esc_html_e('Reports', 'freelance-studio'); ?></a>
    </div>

    <?php if ( function_exists( 'fs_studio_is_free_plan' ) && fs_studio_is_free_plan() ): ?>
        <div class="fs-pro-overlay-wrapper" style="min-height: 300px;">
            <div class="fs-pro-overlay">
                <div class="fs-pro-overlay-content">
                    <span class="fs-upgrade-badge">PRO</span>
                    <h3><?php esc_html_e( 'Time Tracking Reports', 'freelance-studio' ); ?></h3>
                    <p><?php esc_html_e( 'Detailed time reports with client, project, and date breakdowns are available with Freelance Studio Pro.', 'freelance-studio' ); ?></p>
                    <a href="<?php echo function_exists( 'freelance_studio_fs' ) ? esc_url( freelance_studio_fs()->get_upgrade_url() ) : '#'; ?>" class="fs-btn fs-btn-primary">
                        <?php esc_html_e( 'Upgrade to Pro', 'freelance-studio' ); ?>
                    </a>
                </div>
            </div>
            <!-- Placeholder content visible behind overlay -->
            <div aria-hidden="true" style="opacity:0.6;">
                <div class="fs-report-filters" style="pointer-events:none;">
                    <div class="fs-filter-form">
                        <div class="fs-filter-row">
                            <div class="fs-filter-group">
                                <label><?php esc_html_e('From', 'freelance-studio'); ?></label>
                                <input type="date" disabled>
                            </div>
                            <div class="fs-filter-group">
                                <label><?php esc_html_e('To', 'freelance-studio'); ?></label>
                                <input type="date" disabled>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="fs-report-section" style="pointer-events:none;">
                    <h2><?php esc_html_e('Summary', 'freelance-studio'); ?></h2>
                    <table class="widefat fs-table">
                        <thead><tr><th><?php esc_html_e('Metric', 'freelance-studio'); ?></th><th class="text-right"><?php esc_html_e('Value', 'freelance-studio'); ?></th></tr></thead>
                        <tbody>
                            <tr><td><?php esc_html_e('Total Hours', 'freelance-studio'); ?></td><td class="text-right">—</td></tr>
                            <tr><td><?php esc_html_e('Billable Hours', 'freelance-studio'); ?></td><td class="text-right">—</td></tr>
                            <tr><td><?php esc_html_e('Total Earnings', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <?php else: ?>

    <!-- Filters -->
    <div class="fs-report-filters">
        <form method="get" action="" class="fs-filter-form">
            <input type="hidden" name="page" value="fs-time-tracking">
            <input type="hidden" name="action" value="reports">
            
            <div class="fs-filter-row">
                <div class="fs-filter-group">
                    <label><?php esc_html_e('From', 'freelance-studio'); ?></label>
                    <input type="date" name="date_from" value="<?php echo esc_attr($filters['date_from']); ?>">
                </div>
                
                <div class="fs-filter-group">
                    <label><?php esc_html_e('To', 'freelance-studio'); ?></label>
                    <input type="date" name="date_to" value="<?php echo esc_attr($filters['date_to']); ?>">
                </div>
                
                <div class="fs-filter-group">
                    <label><?php esc_html_e('Client', 'freelance-studio'); ?></label>
                    <select name="client_id">
                        <option value=""><?php esc_html_e('All clients', 'freelance-studio'); ?></option>
                        <?php foreach ($clients as $client): ?>
                            <option value="<?php echo esc_attr($client->id); ?>" <?php selected($filters['client_id'], $client->id); ?>>
                                <?php echo esc_html($client->company_name ?: $client->name); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="fs-filter-group">
                    <label><?php esc_html_e('Project', 'freelance-studio'); ?></label>
                    <select name="project_id">
                        <option value=""><?php esc_html_e('All projects', 'freelance-studio'); ?></option>
                        <?php foreach ($projects as $project): ?>
                            <option value="<?php echo esc_attr($project->id); ?>" <?php selected($filters['project_id'], $project->id); ?>>
                                <?php echo esc_html($project->name); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="fs-filter-group">
                    <label><?php esc_html_e('Group by', 'freelance-studio'); ?></label>
                    <select name="group_by">
                        <option value="project" <?php selected($filters['group_by'], 'project'); ?>><?php esc_html_e('Project', 'freelance-studio'); ?></option>
                        <option value="client" <?php selected($filters['group_by'], 'client'); ?>><?php esc_html_e('Client', 'freelance-studio'); ?></option>
                        <option value="task" <?php selected($filters['group_by'], 'task'); ?>><?php esc_html_e('Task', 'freelance-studio'); ?></option>
                        <option value="activity" <?php selected($filters['group_by'], 'activity'); ?>><?php esc_html_e('Activity', 'freelance-studio'); ?></option>
                        <option value="date" <?php selected($filters['group_by'], 'date'); ?>><?php esc_html_e('Date', 'freelance-studio'); ?></option>
                    </select>
                </div>
                
                <div class="fs-filter-group fs-filter-buttons">
                    <button type="submit" class="fs-btn fs-btn-secondary"><?php esc_html_e('Generate Report', 'freelance-studio'); ?></button>
                </div>
            </div>
        </form>
    </div>

    <!-- Summary Stats -->
    <?php if ($summary): ?>
    <div class="fs-report-summary">
        <div class="fs-stat-card">
            <div class="fs-stat-icon"><span class="dashicons dashicons-clock"></span></div>
            <div class="fs-stat-content">
                <div class="fs-stat-value"><?php echo esc_html(FS_Time_Entry::format_duration($summary->total_seconds ?: 0)); ?></div>
                <div class="fs-stat-label"><?php esc_html_e('Total Time', 'freelance-studio'); ?></div>
            </div>
        </div>
        
        <div class="fs-stat-card">
            <div class="fs-stat-icon"><span class="dashicons dashicons-money-alt"></span></div>
            <div class="fs-stat-content">
                <div class="fs-stat-value"><?php echo esc_html(FS_Time_Entry::format_duration($summary->billable_seconds ?: 0)); ?></div>
                <div class="fs-stat-label"><?php esc_html_e('Billable Time', 'freelance-studio'); ?></div>
            </div>
        </div>
        
        <div class="fs-stat-card">
            <div class="fs-stat-icon"><span class="dashicons dashicons-chart-bar"></span></div>
            <div class="fs-stat-content">
                <div class="fs-stat-value"><?php echo  esc_html( FS_View_Context::format_currency($summary->total_billable_amount ?: 0) ); ?></div>
                <div class="fs-stat-label"><?php esc_html_e('Billable Amount', 'freelance-studio'); ?></div>
            </div>
        </div>
        
        <div class="fs-stat-card">
            <div class="fs-stat-icon"><span class="dashicons dashicons-warning"></span></div>
            <div class="fs-stat-content">
                <div class="fs-stat-value"><?php echo  esc_html( FS_View_Context::format_currency($summary->uninvoiced_amount ?: 0) ); ?></div>
                <div class="fs-stat-label"><?php esc_html_e('Uninvoiced', 'freelance-studio'); ?></div>
            </div>
        </div>
    </div>
    <?php endif; ?>

    <!-- Breakdown Table -->
    <div class="fs-report-breakdown">
        <h2><?php esc_html_e('Breakdown', 'freelance-studio'); ?></h2>
        
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th><?php 
                        switch ($filters['group_by']) {
                            case 'client': esc_html_e('Client', 'freelance-studio'); break;
                            case 'task': esc_html_e('Task', 'freelance-studio'); break;
                            case 'activity': esc_html_e('Activity', 'freelance-studio'); break;
                            case 'date': esc_html_e('Date', 'freelance-studio'); break;
                            default: esc_html_e('Project', 'freelance-studio');
                        }
                    ?></th>
                    <?php if ($filters['group_by'] === 'project'): ?>
                        <th><?php esc_html_e('Client', 'freelance-studio'); ?></th>
                    <?php elseif ($filters['group_by'] === 'task'): ?>
                        <th><?php esc_html_e('Project', 'freelance-studio'); ?></th>
                    <?php endif; ?>
                    <th style="width: 80px;"><?php esc_html_e('Entries', 'freelance-studio'); ?></th>
                    <th style="width: 100px;"><?php esc_html_e('Total Time', 'freelance-studio'); ?></th>
                    <th style="width: 100px;"><?php esc_html_e('Billable', 'freelance-studio'); ?></th>
                    <th style="width: 120px;"><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                    <th style="width: 100px;"><?php esc_html_e('Invoiced', 'freelance-studio'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($breakdown)): ?>
                    <tr>
                        <td colspan="<?php echo in_array($filters['group_by'], array('project', 'task')) ? 7 : 6; ?>">
                            <?php esc_html_e('No time entries found for this period.', 'freelance-studio'); ?>
                        </td>
                    </tr>
                <?php else: ?>
                    <?php 
                    $activity_types = FS_Time_Entry::get_activity_types();
                    foreach ($breakdown as $row): 
                    ?>
                        <tr>
                            <td>
                                <strong>
                                <?php 
                                if ($filters['group_by'] === 'activity') {
                                    echo esc_html($activity_types[$row->group_name] ?? $row->group_name);
                                } elseif ($filters['group_by'] === 'date') {
                                    echo esc_html(gmdate('M j, Y', strtotime($row->group_name)));
                                } elseif ($filters['group_by'] === 'client') {
                                    echo esc_html($row->company_name ?: $row->group_name);
                                } elseif ($filters['group_by'] === 'task') {
                                    echo esc_html($row->group_name ?: __('No task', 'freelance-studio'));
                                } else {
                                    echo esc_html($row->group_name ?: __('No project', 'freelance-studio'));
                                }
                                ?>
                                </strong>
                            </td>
                            <?php if ($filters['group_by'] === 'project'): ?>
                                <td><?php echo esc_html($row->client_name ?? '—'); ?></td>
                            <?php elseif ($filters['group_by'] === 'task'): ?>
                                <td><?php echo esc_html($row->project_name ?? '—'); ?></td>
                            <?php endif; ?>
                            <td><?php echo esc_html($row->entry_count); ?></td>
                            <td><?php echo esc_html(FS_Time_Entry::format_duration($row->total_seconds)); ?></td>
                            <td><?php echo esc_html(FS_Time_Entry::format_duration($row->billable_seconds)); ?></td>
                            <td><?php echo  esc_html( FS_View_Context::format_currency($row->billable_amount ?: 0) ); ?></td>
                            <td>
                                <?php 
                                $invoiced_pct = $row->billable_seconds > 0 
                                    ? round(($row->invoiced_seconds / $row->billable_seconds) * 100) 
                                    : 0;
                                ?>
                                <div class="fs-progress-bar">
                                    <div class="fs-progress-fill" style="width: <?php echo esc_attr($invoiced_pct); ?>%;"></div>
                                </div>
                                <small><?php echo esc_html($invoiced_pct); ?>%</small>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
            <?php if (!empty($breakdown)): ?>
            <tfoot>
                <tr>
                    <th><strong><?php esc_html_e('Total', 'freelance-studio'); ?></strong></th>
                    <?php if ($filters['group_by'] === 'project' || $filters['group_by'] === 'task'): ?>
                        <th></th>
                    <?php endif; ?>
                    <th><strong><?php echo esc_html($summary->total_entries ?? 0); ?></strong></th>
                    <th><strong><?php echo esc_html(FS_Time_Entry::format_duration($summary->total_seconds ?: 0)); ?></strong></th>
                    <th><strong><?php echo esc_html(FS_Time_Entry::format_duration($summary->billable_seconds ?: 0)); ?></strong></th>
                    <th><strong><?php echo  esc_html( FS_View_Context::format_currency($summary->total_billable_amount ?: 0) ); ?></strong></th>
                    <th></th>
                </tr>
            </tfoot>
            <?php endif; ?>
        </table>
    </div>
    <?php endif; // end premium gate ?>
</div>
