CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- ======================================================
-- Global Settings (LMS)
-- ======================================================
INSERT INTO global_settings (key, value)
VALUES
    ('platform_name', 'Yimaru LMS'),
    ('default_language', 'en'),
    ('allow_self_signup', 'true'),
    ('otp_expiry_minutes', '5'),
    ('certificate_enabled', 'true'),
    ('max_courses_per_instructor', '50')
ON CONFLICT (key) DO NOTHING;

-- ======================================================
-- Organizations (Tenants)
-- ======================================================
INSERT INTO organizations (
    id,
    name,
    slug,
    owner_id,
    is_active,
    created_at,
    updated_at
)
VALUES (
    1,
    'Yimaru Academy',
    'yimaru-academy',
    1,
    TRUE,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name,
    slug = EXCLUDED.slug,
    is_active = EXCLUDED.is_active,
    updated_at = CURRENT_TIMESTAMP;

-- ======================================================
-- Users
-- Roles: SUPER_ADMIN, ORG_ADMIN, INSTRUCTOR, STUDENT
-- ======================================================
INSERT INTO users (
    id,
    first_name,
    last_name,
    nick_name,
    email,
    phone_number,
    password,
    role,
    age,
    education_level,
    country,
    region,
    email_verified,
    phone_verified,
    suspended,
    organization_id,
    created_at,
    updated_at
)
VALUES
(
    1,
    'Sarah',
    'Connor',
    'SarahC',
    'yaredyemane1@gmail.com',
    NULL,
    crypt('password@123', gen_salt('bf'))::bytea,
    'SUPER_ADMIN',
    35,
    'Masters',
    'USA',
    'California',
    TRUE,
    FALSE,
    FALSE,
    NULL,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
),
(
    2,
    'Test',
    'Instructor',
    'InstructorT',
    'instructor@yimaru.com',
    '0988554466',
    crypt('password@123', gen_salt('bf'))::bytea,
    'INSTRUCTOR',
    30,
    'Bachelors',
    'USA',
    'New York',
    TRUE,
    TRUE,
    FALSE,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
),
(
    3,
    'Demo',
    'Student',
    'DemoS',
    'student@yimaru.com',
    NULL,
    crypt('password@123', gen_salt('bf'))::bytea,
    'STUDENT',
    22,
    'High School',
    'USA',
    'Texas',
    TRUE,
    FALSE,
    FALSE,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO UPDATE
SET first_name = EXCLUDED.first_name,
    last_name = EXCLUDED.last_name,
    nick_name = EXCLUDED.nick_name,
    email = EXCLUDED.email,
    phone_number = EXCLUDED.phone_number,
    password = EXCLUDED.password,
    role = EXCLUDED.role,
    age = EXCLUDED.age,
    education_level = EXCLUDED.education_level,
    country = EXCLUDED.country,
    region = EXCLUDED.region,
    email_verified = EXCLUDED.email_verified,
    phone_verified = EXCLUDED.phone_verified,
    suspended = EXCLUDED.suspended,
    organization_id = EXCLUDED.organization_id,
    updated_at = CURRENT_TIMESTAMP;

-- ======================================================
-- Courses
-- ======================================================
INSERT INTO courses (
    id,
    organization_id,
    instructor_id,
    title,
    description,
    level,
    language,
    is_published,
    created_at,
    updated_at
)
VALUES (
    1,
    1,
    2,
    'Introduction to Go Programming',
    'Learn the fundamentals of Go for backend development.',
    'beginner',
    'en',
    TRUE,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO UPDATE
SET title = EXCLUDED.title,
    description = EXCLUDED.description,
    is_published = EXCLUDED.is_published,
    updated_at = CURRENT_TIMESTAMP;

-- ======================================================
-- Course Modules
-- ======================================================
INSERT INTO course_modules (
    id,
    course_id,
    title,
    position,
    created_at
)
VALUES (
    1,
    1,
    'Getting Started',
    1,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO NOTHING;

-- ======================================================
-- Lessons
-- ======================================================
INSERT INTO lessons (
    id,
    module_id,
    title,
    content_type,
    content_url,
    duration_minutes,
    position,
    created_at
)
VALUES (
    1,
    1,
    'What is Go?',
    'video',
    'https://example.com/go-intro',
    15,
    1,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO NOTHING;

-- ======================================================
-- Enrollments
-- ======================================================
INSERT INTO enrollments (
    id,
    course_id,
    student_id,
    enrolled_at
)
VALUES (
    1,
    1,
    3,
    CURRENT_TIMESTAMP
)
ON CONFLICT (id) DO NOTHING;

-- ======================================================
-- Notifications (Sample)
-- ======================================================
INSERT INTO notifications (
    user_id,
    type,
    level,
    channel,
    title,
    message,
    created_at
)
VALUES (
    3,
    'course_enrolled',
    'info',
    'in_app',
    'Welcome to your course',
    'You have successfully enrolled in Introduction to Go Programming.',
    CURRENT_TIMESTAMP
);
