CREATE TABLE facebook_integrations (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id INT UNSIGNED NOT NULL,
    facebook_user_id VARCHAR(50) NOT NULL,
    facebook_name VARCHAR(150) NOT NULL,
    profile_picture_url VARCHAR(255) NULL,
    access_token TEXT NOT NULL,
    token_expires_at DATETIME NULL,
    granted_scopes TEXT NULL,
    declined_scopes TEXT NULL,
    status ENUM('pending','active','revoked','error') NOT NULL DEFAULT 'pending',
    last_synced_at DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT fk_facebook_integration_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
    UNIQUE KEY idx_facebook_integration_unique (user_id, facebook_user_id),
    INDEX idx_facebook_integration_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
