-- Team members table for managing internal LMS company staff
CREATE TABLE IF NOT EXISTS team_members (
    id BIGSERIAL PRIMARY KEY,
    
    -- Basic info
    first_name VARCHAR(255) NOT NULL,
    last_name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE,
    phone_number VARCHAR(20),
    
    -- Authentication
    password BYTEA NOT NULL,
    
    -- Role within the team (different from learner roles)
    team_role VARCHAR(50) NOT NULL CHECK (
        team_role IN (
            'SUPER_ADMIN',      -- Full system access
            'ADMIN',            -- Administrative tasks
            'CONTENT_MANAGER',  -- Manages courses, content
            'SUPPORT_AGENT',    -- Customer support
            'INSTRUCTOR',       -- Creates/manages courses
            'FINANCE',          -- Payment/subscription management
            'HR',               -- Team member management
            'ANALYST'           -- Reports and analytics
        )
    ),
    
    -- Department
    department VARCHAR(100),
    
    -- Job title
    job_title VARCHAR(150),
    
    -- Employment details
    employment_type VARCHAR(50) CHECK (
        employment_type IN ('full_time', 'part_time', 'contract', 'intern')
    ),
    hire_date DATE,
    
    -- Profile
    profile_picture_url TEXT,
    bio TEXT,
    
    -- Contact
    work_phone VARCHAR(20),
    emergency_contact VARCHAR(255),
    
    -- Status
    status VARCHAR(50) NOT NULL DEFAULT 'active' CHECK (
        status IN ('active', 'inactive', 'suspended', 'terminated')
    ),
    
    -- Verification
    email_verified BOOLEAN NOT NULL DEFAULT FALSE,
    
    -- Permissions (JSON array of permission strings)
    permissions JSONB DEFAULT '[]'::jsonb,
    
    -- Tracking
    last_login TIMESTAMPTZ,
    created_by BIGINT REFERENCES team_members(id),
    updated_by BIGINT REFERENCES team_members(id),
    
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

-- Indexes
CREATE INDEX IF NOT EXISTS idx_team_members_email ON team_members(email);
CREATE INDEX IF NOT EXISTS idx_team_members_team_role ON team_members(team_role);
CREATE INDEX IF NOT EXISTS idx_team_members_department ON team_members(department);
CREATE INDEX IF NOT EXISTS idx_team_members_status ON team_members(status);
