-- Create login_history table to track user logins
CREATE TABLE IF NOT EXISTS login_history (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    login_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    ip_address VARCHAR(45),  -- IPv6 max length is 45 characters
    user_agent TEXT,
    success BOOLEAN NOT NULL DEFAULT TRUE,
    failure_reason VARCHAR(255)
);

-- Create index on user_id for faster queries
CREATE INDEX IF NOT EXISTS idx_login_history_user_id ON login_history(user_id);

-- Create index on login_time for sorting
CREATE INDEX IF NOT EXISTS idx_login_history_login_time ON login_history(login_time DESC);
