-- Super admin account for production / staging
-- Cell: +989127004945 (09127004945)
-- Password: Ali2reza

INSERT INTO users (cell_number, password_hash, email, first_name, last_name, cell_verified_at, is_active)
VALUES (
  '+989127004945',
  '$2b$10$avrNkn5W5gWkZNrupUAXwePyj4FiwQM4H5hvHp.btPA1L0I0NqgAi',
  'ali@meshkee.app',
  'Ali',
  'Reza',
  NOW(),
  TRUE
)
ON CONFLICT (cell_number) DO UPDATE SET
  password_hash = EXCLUDED.password_hash,
  first_name = EXCLUDED.first_name,
  last_name = EXCLUDED.last_name,
  is_active = TRUE,
  cell_verified_at = COALESCE(users.cell_verified_at, NOW());

-- Ensure only super_admin as global role (removes business_owner/customer if present)
DELETE FROM user_roles ur
USING users u, roles r
WHERE ur.user_id = u.id
  AND ur.role_id = r.id
  AND u.cell_number = '+989127004945'
  AND r.slug IN ('super_admin', 'business_owner', 'business_staff', 'customer');

INSERT INTO user_roles (user_id, role_id)
SELECT u.id, r.id
FROM users u
JOIN roles r ON r.slug = 'super_admin'
WHERE u.cell_number = '+989127004945'
ON CONFLICT (user_id, role_id) DO NOTHING;
