<?php
/**
 * Migration Status for Easy Invoice
 *
 * @package     EasyInvoice
 * @subpackage  Migration
 * @since       2.0.0
 */

namespace EasyInvoice\Migration;

/**
 * Migration status class.
 * 
 * Provides comprehensive migration status and information.
 * 
 * @since 2.0.0
 */
class MigrationStatus {
    
    /**
     * Get comprehensive migration status.
     * 
     * @since 2.0.0
     * @return array
     */
    public static function get_status() {
        $old_data = MigrationTest::check_old_plugin_data();
        $test_results = MigrationTest::test_all_migrations();
        
        return [
            'migration_needed' => self::is_migration_needed($old_data),
            'old_data_summary' => [
                'options' => count($old_data['options']),
                'post_types' => $old_data['post_types'],
                'meta_fields' => $old_data['meta'],
            ],
            'migration_components' => [
                'options_migration' => [
                    'status' => 'ready',
                    'description' => 'Options migration component is ready',
                    'old_options_found' => count($old_data['options']),
                ],
                'post_type_migration' => [
                    'status' => 'ready',
                    'description' => 'Post type migration component is ready',
                    'old_post_types_found' => array_keys($old_data['post_types']),
                ],
                'meta_migration' => [
                    'status' => 'ready',
                    'description' => 'Meta migration component is ready',
                    'meta_fields_found' => $old_data['meta'],
                ],
            ],
            'recommendations' => self::get_recommendations($old_data, $test_results),
            'migration_steps' => self::get_migration_steps(),
        ];
    }
    
    /**
     * Check if migration is needed.
     * 
     * @since 2.0.0
     * @param array $old_data
     * @return bool
     */
    private static function is_migration_needed($old_data) {
        return !empty($old_data['options']) || 
               !empty($old_data['post_types']) || 
               !empty($old_data['meta']);
    }
    
    /**
     * Get migration recommendations.
     * 
     * @since 2.0.0
     * @param array $old_data
     * @param array $test_results
     * @return array
     */
    private static function get_recommendations($old_data, $test_results) {
        $recommendations = [];
        
        if (empty($old_data['options']) && empty($old_data['post_types']) && empty($old_data['meta'])) {
            $recommendations[] = 'No old plugin data found. Migration is not needed.';
        } else {
            $recommendations[] = 'Old plugin data found. Migration is recommended.';
            
            if (!empty($old_data['options'])) {
                $recommendations[] = sprintf('Found %d old options that need migration.', count($old_data['options']));
            }
            
            if (!empty($old_data['post_types'])) {
                $recommendations[] = sprintf('Found old post types: %s', implode(', ', array_keys($old_data['post_types'])));
            }
            
            if (!empty($old_data['meta'])) {
                $meta_count = 0;
                foreach ($old_data['meta'] as $post_type => $meta_fields) {
                    $meta_count += count($meta_fields);
                }
                $recommendations[] = sprintf('Found %d meta fields that need migration.', $meta_count);
            }
        }
        
        if (!$test_results['overall']) {
            $recommendations[] = 'Warning: Some migration tests failed. Please check migration components.';
        }
        
        return $recommendations;
    }
    
    /**
     * Get migration steps.
     * 
     * @since 2.0.0
     * @return array
     */
    private static function get_migration_steps() {
        return [
            [
                'step' => 1,
                'title' => 'Options Migration',
                'description' => 'Migrate plugin options from old structure to new structure',
                'status' => 'pending',
                'details' => [
                    'Old options will be mapped to new option names',
                    'Option values will be transformed if needed',
                    'Old options will be preserved as backup',
                ]
            ],
            [
                'step' => 2,
                'title' => 'Post Type Migration',
                'description' => 'Migrate custom post types and their data',
                'status' => 'pending',
                'details' => [
                    'easy-invoice → easy_invoice',
                    'easy-invoice-quotes → easy_quote',
                    'easy-invoice-payment → easy_payment',
                    'Post meta will be migrated with post types',
                ]
            ],
            [
                'step' => 3,
                'title' => 'Meta Data Migration',
                'description' => 'Migrate post meta data to new structure',
                'status' => 'pending',
                'details' => [
                    'Meta keys will be prefixed with _easy_invoice_',
                    'Meta values will be transformed if needed',
                    'Old meta will be preserved as backup',
                ]
            ],
            [
                'step' => 4,
                'title' => 'Verification',
                'description' => 'Verify all data has been migrated correctly',
                'status' => 'pending',
                'details' => [
                    'Check that all options are accessible',
                    'Verify post types are working correctly',
                    'Test meta data retrieval',
                    'Ensure no data loss occurred',
                ]
            ],
        ];
    }
    
    /**
     * Get detailed migration report.
     * 
     * @since 2.0.0
     * @return array
     */
    public static function get_detailed_report() {
        $status = self::get_status();
        $old_data = MigrationTest::check_old_plugin_data();
        
        return [
            'summary' => $status,
            'old_data_details' => [
                'options' => $old_data['options'],
                'post_types' => $old_data['post_types'],
                'meta' => $old_data['meta'],
            ],
            'migration_mappings' => [
                'options' => self::get_options_mappings(),
                'post_types' => self::get_post_type_mappings(),
                'meta' => self::get_meta_mappings(),
            ],
            'estimated_time' => self::estimate_migration_time($old_data),
            'backup_recommendation' => 'Always backup your database before running migration.',
        ];
    }
    
    /**
     * Get options mappings.
     * 
     * @since 2.0.0
     * @return array
     */
    private static function get_options_mappings() {
        return [
            'Business Settings' => [
                'easy_invoice_business_name' => 'easy_invoice_company_name',
                'easy_invoice_business_address' => 'easy_invoice_company_address',
                'easy_invoice_business_logo' => 'easy_invoice_company_logo',
            ],
            'Invoice Settings' => [
                'easy_invoice_invoice_number' => 'easy_invoice_next_invoice_number',
                'easy_invoice_number_prefix' => 'easy_invoice_invoice_prefix',
            ],
            'Quote Settings' => [
                'easy_invoice_quote_number' => 'easy_invoice_next_quote_number',
                'easy_invoice_quote_number_prefix' => 'easy_invoice_quote_prefix',
            ],
            'Payment Settings' => [
                'easy_invoice_payment_gateway_paypal_email' => 'easy_invoice_paypal_email',
            ],
            'Email Settings' => [
                'easy_invoice_email_from_address' => 'easy_invoice_email_from_address',
                'easy_invoice_email_from_name' => 'easy_invoice_email_from_name',
            ],
        ];
    }
    
    /**
     * Get post type mappings.
     * 
     * @since 2.0.0
     * @return array
     */
    private static function get_post_type_mappings() {
        return [
            'easy-invoice' => 'easy_invoice',
            'easy-invoice-quotes' => 'easy_quote',
            'easy-invoice-payment' => 'easy_payment',
        ];
    }
    
    /**
     * Get meta mappings.
     * 
     * @since 2.0.0
     * @return array
     */
    private static function get_meta_mappings() {
        return [
            'easy-invoice' => [
                'invoice_status' => '_easy_invoice_status',
                'invoice_number' => '_easy_invoice_number',
                'client_email' => '_easy_invoice_customer_email',
                'client_name' => '_easy_invoice_customer_name',
            ],
            'easy-invoice-quotes' => [
                'quote_status' => '_easy_invoice_quote_status',
                'quote_number' => '_easy_invoice_quote_number',
                'client_email' => '_easy_invoice_quote_customer_email',
                'client_name' => '_easy_invoice_quote_customer_name',
            ],
            'easy-invoice-payment' => [
                'payment_amount' => '_easy_invoice_payment_amount',
                'payment_method' => '_easy_invoice_payment_method',
                'payment_status' => '_easy_invoice_payment_status',
            ],
        ];
    }
    
    /**
     * Estimate migration time.
     * 
     * @since 2.0.0
     * @param array $old_data
     * @return string
     */
    private static function estimate_migration_time($old_data) {
        $total_items = count($old_data['options']);
        
        foreach ($old_data['post_types'] as $post_type => $count) {
            $total_items += $count;
        }
        
        foreach ($old_data['meta'] as $post_type => $meta_fields) {
            $total_items += count($meta_fields);
        }
        
        if ($total_items < 100) {
            return 'Less than 1 minute';
        } elseif ($total_items < 1000) {
            return '1-5 minutes';
        } else {
            return '5-15 minutes';
        }
    }
} 