<?php
/**
 * Finances - Tax Page
 * 
 * Consolidates tax settings, annual tax summary, quarterly estimates,
 * annual deductions, and quarterly payments into one page.
 * Variables are prepared by FS_Finances_Controller::render_tax().
 *
 * @package Freelance_Studio
 */

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

$is_free = function_exists('fs_studio_is_free_plan') && fs_studio_is_free_plan();
$is_pro = function_exists('fs_studio_can_use_premium') && fs_studio_can_use_premium();
$upgrade_url = function_exists('freelance_studio_fs') ? freelance_studio_fs()->get_upgrade_url() : '#';
?>

<?php if ($show_success): ?>
    <div class="notice notice-success is-dismissible">
        <p><?php esc_html_e('Settings saved.', 'freelance-studio'); ?></p>
    </div>
<?php endif; ?>

<!-- Year Selector -->
<div class="fs-year-filter" style="margin-bottom: 20px;">
    <form method="get">
        <input type="hidden" name="page" value="fs-finances">
        <input type="hidden" name="tab" value="tax">
        <label for="year"><strong><?php esc_html_e('Tax Year:', 'freelance-studio'); ?></strong></label>
        <select name="year" id="year" onchange="this.form.submit()">
            <?php foreach ($available_years as $year): ?>
                <option value="<?php echo esc_attr($year); ?>" <?php selected($selected_year, $year); ?>><?php echo esc_html($year); ?></option>
            <?php endforeach; ?>
        </select>
    </form>
</div>

<?php if ($is_free): ?>
<!-- ===== FREE PLAN: Show pro overlay ===== -->
<div class="fs-settings-section fs-pro-overlay-wrapper" style="min-height: 400px;">
    <div class="fs-pro-overlay">
        <div class="fs-pro-overlay-content">
            <span class="fs-upgrade-badge">PRO</span>
            <h3><?php esc_html_e('Tax Planning & Estimates', 'freelance-studio'); ?></h3>
            <p><?php esc_html_e('Quarterly tax estimates, annual tax summary, deduction tracking, state/federal bracket configuration, and payment reminders are available with Freelance Studio Pro.', 'freelance-studio'); ?></p>
            <a href="<?php echo esc_url($upgrade_url); ?>" class="fs-btn fs-btn-primary fs-btn-small">
                <?php esc_html_e('Upgrade to Pro', 'freelance-studio'); ?>
            </a>
        </div>
    </div>
    <div aria-hidden="true" style="opacity:0.5; pointer-events:none;">
        <h2><?php esc_html_e('Tax Settings', 'freelance-studio'); ?></h2>
        <p class="description mb-15"><?php esc_html_e('Used for quarterly tax estimates. This is not tax advice.', 'freelance-studio'); ?></p>
        <table class="form-table">
            <tr>
                <th scope="row"><label><?php esc_html_e('State', 'freelance-studio'); ?></label></th>
                <td><select disabled class="regular-text"><option><?php esc_html_e('Select your state...', 'freelance-studio'); ?></option></select></td>
            </tr>
            <tr>
                <th scope="row"><label><?php esc_html_e('Federal Tax Bracket', 'freelance-studio'); ?></label></th>
                <td><select disabled class="regular-text"><option><?php esc_html_e('Select...', 'freelance-studio'); ?></option></select></td>
            </tr>
        </table>
        <h2 style="margin-top:30px;"><?php esc_html_e('Annual Tax Summary', 'freelance-studio'); ?></h2>
        <table class="widefat fs-table">
            <tbody>
                <tr><td><?php esc_html_e('Gross Income', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                <tr><td><?php esc_html_e('Tracked Expenses', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                <tr><td class="font-bold"><?php esc_html_e('Net Taxable Income', 'freelance-studio'); ?></td><td class="text-right font-bold">$—</td></tr>
                <tr><td colspan="2"><hr class="fs-hr"></td></tr>
                <tr><td><?php esc_html_e('Self-Employment Tax', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                <tr><td><?php esc_html_e('Federal Income Tax', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                <tr><td><?php esc_html_e('State Income Tax', 'freelance-studio'); ?></td><td class="text-right">$—</td></tr>
                <tr><td class="font-bold"><?php esc_html_e('Total Estimated Tax', 'freelance-studio'); ?></td><td class="text-right font-bold">$—</td></tr>
            </tbody>
        </table>
    </div>
</div>

<?php else: ?>
<!-- ===== PRO PLAN: Full Tax Page ===== -->

<!-- Tax Settings -->
<div class="fs-settings-section">
    <h2><?php esc_html_e('Tax Settings', 'freelance-studio'); ?></h2>
    <p class="description mb-15"><?php esc_html_e('Used for quarterly tax estimates. This is not tax advice.', 'freelance-studio'); ?></p>
    
    <div class="fs-tax-disclaimer">
        <span class="dashicons dashicons-warning"></span>
        <p><?php echo wp_kses_post(__('<strong>Important:</strong> All tax calculations provided are estimates only and should not be relied upon for actual tax filing. Always consult with a qualified tax professional or CPA for accurate tax advice.', 'freelance-studio')); ?></p>
    </div>
    
    <form method="post" action="">
        <?php wp_nonce_field('fs_save_settings', 'fs_settings_nonce'); ?>
        <input type="hidden" name="tab" value="tax">
        
        <table class="form-table">
            <tr>
                <th scope="row"><label for="tax_state"><?php esc_html_e('State', 'freelance-studio'); ?></label></th>
                <td>
                    <select name="fs_tax_state" id="tax_state" class="regular-text">
                        <option value=""><?php esc_html_e('Select your state...', 'freelance-studio'); ?></option>
                        <?php foreach ($states as $code => $name): ?>
                            <option value="<?php echo esc_attr($code); ?>" <?php selected($tax_state, $code); ?>><?php echo esc_html($name); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th scope="row"><label for="federal_tax_bracket"><?php esc_html_e('Federal Tax Bracket', 'freelance-studio'); ?></label></th>
                <td>
                    <select name="fs_federal_tax_bracket" id="federal_tax_bracket" class="regular-text">
                        <option value=""><?php esc_html_e('Select...', 'freelance-studio'); ?></option>
                        <?php
                        $brackets = array(
                            '10' => '10% - up to $11,600',
                            '12' => '12% - $11,601-$47,150',
                            '22' => '22% - $47,151-$100,525',
                            '24' => '24% - $100,526-$191,950',
                            '32' => '32% - $191,951-$243,725',
                            '35' => '35% - $243,726-$609,350',
                            '37' => '37% - over $609,350'
                        );
                        foreach ($brackets as $rate => $label):
                        ?>
                            <option value="<?php echo esc_attr($rate); ?>" <?php selected($federal_bracket, $rate); ?>><?php echo esc_html($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
        </table>
        
        <p class="submit">
            <input type="submit" name="submit" class="button button-primary" value="<?php esc_attr_e('Save Tax Settings', 'freelance-studio'); ?>">
        </p>
    </form>
</div>

<!-- Annual Tax Summary -->
<div class="fs-settings-section">
    <h2><?php esc_html_e('Annual Tax Summary', 'freelance-studio'); ?></h2>
    <table class="widefat fs-table">
        <tbody>
            <tr>
                <td><?php esc_html_e('Gross Income', 'freelance-studio'); ?></td>
                <td class="text-right">$<?php echo number_format($tax_data['calculation']['gross_income'], 2); ?></td>
            </tr>
            <tr>
                <td><?php esc_html_e('Tracked Expenses', 'freelance-studio'); ?></td>
                <td class="text-right">-$<?php echo number_format($tax_data['calculation']['expenses'], 2); ?></td>
            </tr>
            <?php if ($tax_data['calculation']['annual_deductions'] > 0): ?>
            <tr>
                <td><?php esc_html_e('Annual Estimated Deductions', 'freelance-studio'); ?></td>
                <td class="text-right">-$<?php echo number_format($tax_data['calculation']['annual_deductions'], 2); ?></td>
            </tr>
            <tr>
                <td class="font-bold"><?php esc_html_e('Total Deductions', 'freelance-studio'); ?></td>
                <td class="text-right font-bold">-$<?php echo number_format($tax_data['calculation']['total_deductions'], 2); ?></td>
            </tr>
            <?php endif; ?>
            <tr>
                <td class="font-bold"><?php esc_html_e('Net Taxable Income', 'freelance-studio'); ?></td>
                <td class="text-right font-bold">$<?php echo number_format($tax_data['calculation']['net_income'], 2); ?></td>
            </tr>
            <tr><td colspan="2"><hr class="fs-hr"></td></tr>
            <tr>
                <td><?php esc_html_e('Self-Employment Tax (15.3%)', 'freelance-studio'); ?></td>
                <td class="text-right">$<?php echo number_format($tax_data['calculation']['tax_breakdown']['self_employment_tax'], 2); ?></td>
            </tr>
            <tr>
                <td><?php esc_html_e('Federal Income Tax', 'freelance-studio'); ?></td>
                <td class="text-right">$<?php echo number_format($tax_data['calculation']['tax_breakdown']['federal_tax'], 2); ?></td>
            </tr>
            <tr>
                <td><?php esc_html_e('State Income Tax', 'freelance-studio'); ?></td>
                <td class="text-right">$<?php echo number_format($tax_data['calculation']['tax_breakdown']['state_tax'], 2); ?></td>
            </tr>
            <tr class="fs-tax-row highlight">
                <td class="font-bold"><?php esc_html_e('Total Estimated Tax', 'freelance-studio'); ?></td>
                <td class="text-right font-bold">$<?php echo number_format($tax_data['calculation']['tax_breakdown']['total_tax'], 2); ?></td>
            </tr>
            <tr><td colspan="2"><hr class="fs-hr"></td></tr>
            <tr>
                <td><?php esc_html_e('Quarterly Payments Made', 'freelance-studio'); ?></td>
                <td class="text-right fs-text-accent">$<?php echo number_format($tax_data['payments']['total_paid'], 2); ?></td>
            </tr>
            <tr class="fs-tax-row warning-bg">
                <td class="font-bold"><?php esc_html_e('Remaining Tax Liability', 'freelance-studio'); ?></td>
                <td class="text-right font-bold fs-text-accent">$<?php echo number_format($tax_data['remaining'], 2); ?></td>
            </tr>
        </tbody>
    </table>
</div>

<!-- Quarterly Tax Estimates (from reporting partial) -->
<?php 
if (file_exists(JEEB_FS_PLUGIN_DIR . 'includes/views/reporting/quarterly-tax-section.php')) {
    include JEEB_FS_PLUGIN_DIR . 'includes/views/reporting/quarterly-tax-section.php';
}
?>

<!-- Annual Tax Deductions -->
<div class="fs-settings-section">
    <h2><?php esc_html_e('Annual Tax Deductions', 'freelance-studio'); ?></h2>
    <p class="description mb-15"><?php esc_html_e('Track annual business deductions that reduce your taxable income.', 'freelance-studio'); ?></p>
    
    <!-- Add/Edit Deduction Form -->
    <?php if ($editing_deduction): ?>
    <div class="fs-deduction-form-wrapper">
        <h3><?php esc_html_e('Edit Deduction', 'freelance-studio'); ?></h3>
        <form method="post" action="" class="fs-deduction-form">
            <?php wp_nonce_field('fs_save_annual_deduction', 'fs_deduction_nonce'); ?>
            <input type="hidden" name="deduction_id" value="<?php echo intval($editing_deduction->id); ?>">
            <input type="hidden" name="deduction_year" value="<?php echo esc_attr($selected_year); ?>">
            <div class="fs-form-row">
                <div class="fs-form-field">
                    <label for="deduction_category"><?php esc_html_e('Category', 'freelance-studio'); ?> <span class="fs-required">*</span></label>
                    <select name="deduction_category" id="deduction_category" required>
                        <?php foreach ($deduction_categories as $cat_key => $cat_label): ?>
                            <option value="<?php echo esc_attr($cat_key); ?>" <?php selected($editing_deduction->category, $cat_key); ?>><?php echo esc_html($cat_label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="fs-form-field">
                    <label for="deduction_name"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                    <input type="text" name="deduction_name" id="deduction_name" value="<?php echo esc_attr($editing_deduction->name); ?>" class="regular-text">
                </div>
                <div class="fs-form-field">
                    <label for="deduction_amount"><?php esc_html_e('Annual Amount', 'freelance-studio'); ?> <span class="fs-required">*</span></label>
                    <input type="number" name="deduction_amount" id="deduction_amount" value="<?php echo esc_attr($editing_deduction->annual_amount); ?>" step="0.01" min="0" required class="regular-text">
                </div>
                <div class="fs-form-field fs-form-actions">
                    <button type="submit" class="button button-primary"><?php esc_html_e('Update', 'freelance-studio'); ?></button>
                    <a href="<?php echo esc_url(admin_url('admin.php?page=fs-finances&tab=tax&year=' . $selected_year)); ?>" class="button"><?php esc_html_e('Cancel', 'freelance-studio'); ?></a>
                </div>
            </div>
        </form>
    </div>
    <?php else: ?>
    <div class="fs-deduction-form-wrapper">
        <form method="post" action="" class="fs-deduction-form">
            <?php wp_nonce_field('fs_save_annual_deduction', 'fs_deduction_nonce'); ?>
            <input type="hidden" name="deduction_year" value="<?php echo esc_attr($selected_year); ?>">
            <div class="fs-form-row">
                <div class="fs-form-field">
                    <label for="deduction_category"><?php esc_html_e('Category', 'freelance-studio'); ?> <span class="fs-required">*</span></label>
                    <select name="deduction_category" id="deduction_category" required>
                        <option value=""><?php esc_html_e('Select...', 'freelance-studio'); ?></option>
                        <?php foreach ($deduction_categories as $cat_key => $cat_label): ?>
                            <option value="<?php echo esc_attr($cat_key); ?>"><?php echo esc_html($cat_label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="fs-form-field">
                    <label for="deduction_name"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                    <input type="text" name="deduction_name" id="deduction_name" class="regular-text" placeholder="<?php esc_attr_e('e.g., Adobe Creative Cloud', 'freelance-studio'); ?>">
                </div>
                <div class="fs-form-field">
                    <label for="deduction_amount"><?php esc_html_e('Annual Amount', 'freelance-studio'); ?> <span class="fs-required">*</span></label>
                    <input type="number" name="deduction_amount" id="deduction_amount" step="0.01" min="0" required class="regular-text" placeholder="0.00">
                </div>
                <div class="fs-form-field fs-form-actions">
                    <button type="submit" class="button button-primary"><?php esc_html_e('Add Deduction', 'freelance-studio'); ?></button>
                </div>
            </div>
        </form>
    </div>
    
    <!-- Quick-add presets -->
    <?php if (!empty($deduction_presets)): ?>
    <div class="fs-deduction-presets">
        <span class="fs-presets-label"><?php esc_html_e('Quick add:', 'freelance-studio'); ?></span>
        <?php foreach ($deduction_presets as $preset): ?>
            <form method="post" action="" style="display:inline;">
                <?php wp_nonce_field('fs_save_annual_deduction', 'fs_deduction_nonce'); ?>
                <input type="hidden" name="deduction_year" value="<?php echo esc_attr($selected_year); ?>">
                <input type="hidden" name="deduction_category" value="<?php echo esc_attr($preset['category']); ?>">
                <input type="hidden" name="deduction_name" value="<?php echo esc_attr($preset['description']); ?>">
                <input type="hidden" name="deduction_amount" value="<?php echo esc_attr($preset['typical_amount']); ?>">
                <input type="hidden" name="deduction_percentage" value="<?php echo esc_attr($preset['typical_percentage']); ?>">
                <button type="submit" class="button button-small"><?php echo esc_html($preset['name']); ?></button>
            </form>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
    <?php endif; ?>
    
    <!-- Deductions Table -->
    <?php if (!empty($annual_deductions)): ?>
    <div class="fs-table-responsive" style="margin-top: 15px;">
        <table class="widefat fs-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Category', 'freelance-studio'); ?></th>
                    <th><?php esc_html_e('Description', 'freelance-studio'); ?></th>
                    <th class="text-right"><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                    <th style="width:100px;"><?php esc_html_e('Actions', 'freelance-studio'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($annual_deductions as $deduction): 
                    $cat_label = isset($deduction_categories[$deduction->category]) ? $deduction_categories[$deduction->category] : $deduction->category;
                ?>
                    <tr>
                        <td><?php echo esc_html($cat_label); ?></td>
                        <td><?php echo esc_html($deduction->name); ?></td>
                        <td class="text-right">$<?php echo number_format($deduction->annual_amount, 2); ?></td>
                        <td>
                            <a href="<?php echo esc_url(admin_url('admin.php?page=fs-finances&tab=tax&year=' . $selected_year . '&edit_deduction=' . intval($deduction->id))); ?>"><?php esc_html_e('Edit', 'freelance-studio'); ?></a> |
                            <a href="#" class="fs-delete-btn text-danger" data-type="annual_deduction" data-id="<?php echo intval($deduction->id); ?>"><?php esc_html_e('Delete', 'freelance-studio'); ?></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="2" class="text-right"><strong><?php esc_html_e('Total Deductible:', 'freelance-studio'); ?></strong></td>
                    <td class="text-right"><strong>$<?php echo number_format($total_deductions, 2); ?></strong></td>
                    <td></td>
                </tr>
            </tfoot>
        </table>
    </div>
    <?php else: ?>
    <div class="fs-empty-state" style="margin-top: 15px;">
        <p><?php esc_html_e('No annual deductions added for this year. Use the form above or quick-add buttons to get started.', 'freelance-studio'); ?></p>
    </div>
    <?php endif; ?>
    
    <!-- Copy from Previous Year -->
    <?php 
    $prev_year = $selected_year - 1;
    $prev_year_count = FS_Annual_Deduction::count($prev_year);
    if ($prev_year_count > 0 && empty($annual_deductions)):
    ?>
    <div class="fs-copy-deductions" style="margin-top: 15px;">
        <form method="post" action="">
            <?php wp_nonce_field('fs_copy_deductions', 'fs_copy_nonce'); ?>
            <input type="hidden" name="from_year" value="<?php echo esc_attr($prev_year); ?>">
            <input type="hidden" name="to_year" value="<?php echo esc_attr($selected_year); ?>">
            <p>
                <?php /* translators: 1. count/number, 2. year */ ?>
                <?php echo esc_html(sprintf(__('You have %1$d deductions from %2$s.', 'freelance-studio'), $prev_year_count, $prev_year)); ?>
                <button type="submit" name="copy_deductions" class="button">
                    <?php /* translators: 1. year */ ?>
                    <?php echo wp_kses_post(sprintf(__('Copy from %s', 'freelance-studio'), $prev_year)); ?>
                </button>
            </p>
        </form>
    </div>
    <?php endif; ?>
</div>

<?php endif; // end pro plan ?>
