<?php

use App\Database\Migration;

/**
 * Migration 019 – Create remember-me tokens table (v3.0.4)
 *
 * Stores persistent login tokens for the "Stay logged in for 14 days" feature.
 * Uses selector:validator pattern for secure token lookup.
 */
class CreateRememberTokens extends Migration
{
    public function up(): void
    {
        $this->db->query("
            CREATE TABLE IF NOT EXISTS iw_remember_tokens (
                id               INT AUTO_INCREMENT PRIMARY KEY,
                user_id          INT NOT NULL,
                selector         VARCHAR(24)  NOT NULL,
                hashed_validator VARCHAR(64)  NOT NULL,
                expires_at       DATETIME     NOT NULL,
                created_at       TIMESTAMP    DEFAULT CURRENT_TIMESTAMP,
                UNIQUE KEY idx_selector (selector),
                KEY idx_user_id (user_id),
                KEY idx_expires_at (expires_at),
                CONSTRAINT fk_remember_user FOREIGN KEY (user_id) REFERENCES iw_users(id) ON DELETE CASCADE
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");
    }

    public function down(): void
    {
        $this->db->query("DROP TABLE IF EXISTS iw_remember_tokens");
    }
}
