<?php
namespace FleekDash\Database\Migrations;

use FleekDash\Database\Core\BaseMigration;
use FleekDash\Database\Core\TableBuilder;

/**
 * Create Site Growth Tracking Table
 *
 * Creates the fleekdash_site_growth_tracking table for individual event tracking.
 * This table stores each occurrence of an event with timestamp, user, and points earned.
 *
 * Use cases:
 * - Track individual event occurrences by date
 * - Enable daily/weekly analytics
 * - Provide audit trail for manual events
 * - Support future advanced analytics (trends, comparisons)
 *
 * @package FleekDash\Database\Migrations
 * @since 2.2.0
 * @date 26-Nov-2025
 */
return new class extends BaseMigration {

    public function up(): void
    {
        // Create site_growth_tracking table
        $this->createTable('fleekdash_site_growth_tracking', function (TableBuilder $table) {
            $table->id();
            $table->unsignedBigInteger('event_id')->comment('Reference to growth event');
            $table->unsignedBigInteger('user_id')->nullable()->comment('User who triggered event (null for system events)');
            $table->integer('points_earned')->default(0)->comment('Points earned for this occurrence');
            $table->datetime('tracked_at')->comment('When event was tracked');
            $table->longText('metadata')->nullable()->comment('JSON metadata for event context');
            $table->timestamps();

            // Indexes for performance
            $table->index('event_id');
            $table->index('user_id');
            $table->index('tracked_at');
            $table->index(['event_id', 'tracked_at'], 'event_date_idx');
        });

        // Update migration status
        update_option('fleekdash_site_growth_tracking_version', '1.0.0');
        update_option('fleekdash_site_growth_tracking_installed_at', current_time('mysql'));

        fleekdash_log_debug('Migration: Successfully created fleekdash_site_growth_tracking table');
    }

    public function down(): void
    {
        $this->dropTable('fleekdash_site_growth_tracking');
        delete_option('fleekdash_site_growth_tracking_version');
        delete_option('fleekdash_site_growth_tracking_installed_at');

        fleekdash_log_debug('Migration: Dropped fleekdash_site_growth_tracking table');
    }
};
