-- Add pack-level likes/views/downloads for user 144's packs
-- 5-10 engagements per pack from other users (44-63)

DROP PROCEDURE IF EXISTS seed_pack_engagement;

DELIMITER $$

CREATE PROCEDURE seed_pack_engagement()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE p_id INT;
  DECLARE eng_count INT;
  DECLARE eng_i INT;
  DECLARE other_user INT;
  DECLARE other_users JSON;

  DECLARE cur CURSOR FOR SELECT id FROM pack WHERE user_id = 144;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  SET other_users = JSON_ARRAY(44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63);

  OPEN cur;
  read_loop: LOOP
    FETCH cur INTO p_id;
    IF done THEN LEAVE read_loop; END IF;

    SET eng_count = 5 + FLOOR(RAND() * 6);
    SET eng_i = 0;
    WHILE eng_i < eng_count DO
      SET other_user = JSON_EXTRACT(other_users, CONCAT('$[', eng_i MOD 20, ']'));

      INSERT IGNORE INTO pack_likes (pack_id, user_id, createdAt, updatedAt)
      VALUES (p_id, other_user, NOW(), NOW());

      INSERT INTO pack_views (pack_id, user_id, createdAt, updatedAt)
      VALUES (p_id, other_user, NOW(), NOW());

      INSERT INTO pack_downloads (pack_id, user_id, createdAt, updatedAt)
      VALUES (p_id, other_user, NOW(), NOW());

      SET eng_i = eng_i + 1;
    END WHILE;
  END LOOP;
  CLOSE cur;
END$$

DELIMITER ;

CALL seed_pack_engagement();
DROP PROCEDURE IF EXISTS seed_pack_engagement;
