CREATE TABLE IF NOT EXISTS bazzar_pos.employees (
    id BIGSERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    role VARCHAR(100) NOT NULL,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_employees_active_name
    ON bazzar_pos.employees(is_active, name);

INSERT INTO bazzar_pos.permissions (key, label, module)
VALUES
    ('employees.view', 'Employees View', 'employees'),
    ('employees.manage', 'Employees Manage', 'employees')
ON CONFLICT (key) DO NOTHING;

INSERT INTO bazzar_pos.role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM bazzar_pos.roles r
JOIN bazzar_pos.permissions p ON p.key IN ('employees.view', 'employees.manage')
WHERE r.code = 'superadmin'
ON CONFLICT DO NOTHING;
