-- =============================================
-- Bayi Aktivite Logu & Aktif Oturumlar
-- Migration Script
-- Tarih: 2026-02-05
-- =============================================

-- 1. Urun Goruntuleme / Arama Logu
CREATE TABLE MS_B2B_PRODUCT_VIEWS (
    LOGICALREF INT IDENTITY(1,1) PRIMARY KEY,
    MEMBERREF INT NOT NULL,
    STOCKREF INT DEFAULT 0,
    ITEMCODE NVARCHAR(100) DEFAULT '',
    ITEMNAME NVARCHAR(250) DEFAULT '',
    SEARCHQUERY NVARCHAR(250) DEFAULT '',
    RESULTCOUNT INT DEFAULT 0,
    IPADDRESS NVARCHAR(50) DEFAULT '',
    VIEWDATE DATETIME DEFAULT GETDATE()
);

CREATE INDEX IX_PRODUCT_VIEWS_MEMBER ON MS_B2B_PRODUCT_VIEWS(MEMBERREF);
CREATE INDEX IX_PRODUCT_VIEWS_DATE ON MS_B2B_PRODUCT_VIEWS(VIEWDATE DESC);
CREATE INDEX IX_PRODUCT_VIEWS_STOCK ON MS_B2B_PRODUCT_VIEWS(STOCKREF);

-- 2. Aktif Oturumlar
CREATE TABLE MS_B2B_ACTIVE_SESSIONS (
    LOGICALREF INT IDENTITY(1,1) PRIMARY KEY,
    MEMBERREF INT NOT NULL,
    USERTYPE TINYINT DEFAULT 1,
    USERNAME NVARCHAR(100) DEFAULT '',
    COMPANYNAME NVARCHAR(250) DEFAULT '',
    SESSION_ID NVARCHAR(128) NOT NULL,
    IPADDRESS NVARCHAR(50) DEFAULT '',
    LAST_ACTIVITY DATETIME DEFAULT GETDATE(),
    LOGIN_TIME DATETIME DEFAULT GETDATE(),
    CURRENT_PAGE NVARCHAR(250) DEFAULT ''
);

CREATE INDEX IX_ACTIVE_SESSIONS_ACTIVITY ON MS_B2B_ACTIVE_SESSIONS(LAST_ACTIVITY);
CREATE UNIQUE INDEX IX_ACTIVE_SESSIONS_SID ON MS_B2B_ACTIVE_SESSIONS(SESSION_ID);
