-- Migration: Create activity_logs table
-- Description: Stores activity logs for auditing and monitoring

CREATE TABLE IF NOT EXISTS activity_logs (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES users(id) ON DELETE SET NULL,
    tenant_id VARCHAR(36),  -- For multi-tenant tracking
    
    -- Activity Info
    action VARCHAR(100) NOT NULL,  -- e.g., 'user.login', 'job.create', 'application.submit'
    resource_type VARCHAR(50),     -- e.g., 'user', 'job', 'application', 'company'
    resource_id VARCHAR(50),       -- ID of the affected resource
    
    -- Details
    description TEXT,
    metadata JSONB,  -- Additional context data
    ip_address VARCHAR(45),
    user_agent TEXT,
    
    -- Metadata
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indexes for efficient querying
CREATE INDEX idx_activity_logs_user ON activity_logs(user_id);
CREATE INDEX idx_activity_logs_tenant ON activity_logs(tenant_id);
CREATE INDEX idx_activity_logs_action ON activity_logs(action);
CREATE INDEX idx_activity_logs_resource ON activity_logs(resource_type, resource_id);
CREATE INDEX idx_activity_logs_created ON activity_logs(created_at DESC);

COMMENT ON TABLE activity_logs IS 'Audit log of all system activities';
