function arrivals = TRI_CY_arrivals(lambda,T,priority_prob,d_mu,d_var, d_min)
% TRI_sim_arrivals - generate a patients arrivals and surgery data
% On input:
%     lambda (float): arrival rate of ED urgent cases
%     T (float): max time for simulation period
%     priority_prob (5x1 vector): probabilities of urgency (low to high)
%     d_mu (float): mean duration of surgery
%     d_var (float): variance of duration of surgery
%     type(int): the generate type
% On output:
%     arrivals (nx3 array): Emergency Health Records
%       col 1: arrival time
%       col 2: priority (5 slots)
%       col 3: surgery duration
% Call:
%      arrivals = TRI_CY_arrivals(2,24,ones(5,1)/5,1,1,1);
% Author:
%     Chengyu Yang
%     UU
%     Fall 2023
%

    
    % get the arrive time for patient
    arrive_time = (TRI_Poisson(lambda, T)).';

    % generate the patient number
    patient_number = size(arrive_time, 1);


    % Calculate the parameters of the lognormal distribution
    mu_log = log((d_mu^2) / sqrt(d_var + d_mu^2));
    sigma_log = sqrt(log(1 + (d_var / d_mu^2)));

    % Generating surgery duration using lognormal distribution
    surgery_duration = lognrnd(mu_log, sigma_log, patient_number, 1);
    surgery_duration(surgery_duration < d_min) = d_min;

    % initialize the EMR with arrive time and surgery duration
    arrivals = zeros(patient_number, 3);
    arrivals(:, 1) = arrive_time;
    arrivals(:, 3) = surgery_duration;

    % initialize the priority of every patient
    for i = 1:patient_number
        temp = rand;
        sum = 0;
        for priority = 1:1:size(priority_prob, 1)
            sum = sum + priority_prob(priority);
            if temp < sum
                arrivals(i, 2) = priority;
                break;
            end
        end
    end