-- FUNCTION: public.udf_provider_availability_visitType1(integer, integer, integer, integer)

-- DROP FUNCTION IF EXISTS public."udf_provider_availability_visitType1"(integer, integer, integer, integer);

CREATE OR REPLACE FUNCTION public."udf_provider_availability_visitType1"(
	providerid integer,
	specializationid integer,
	locationid integer,
	visittypeid integer)
    RETURNS TABLE("ProviderAvailabilityVisitTypeId" integer, "VisitTypeId" integer, "VisitorName" character varying, "Duration" integer) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin

IF EXISTS (select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
		   where PA."ProviderId" = providerid and PA."SpecializationId" = specializationid and PA."LocationId" = locationid
		 AND PA."VisitTypeId" = visittypeid

	) THEN  return query select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
	where PA."ProviderId" = providerid and PA."SpecializationId" = specializationid and PA."LocationId" = locationid
		 AND PA."VisitTypeId" = visittypeid;
--END IF; 1

ELSEIF EXISTS (select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
where  PA."SpecializationId" = specializationid and PA."LocationId" = locationid and PA."ProviderId" is null
	 AND PA."VisitTypeId" = visittypeid

) THEN  return query select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
where  PA."SpecializationId" = specializationid and PA."LocationId" = locationid and PA."ProviderId" is null
	 AND PA."VisitTypeId" = visittypeid;
--END IF; 2
	
ELSEIF EXISTS (select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
			   
where PA."ProviderId" = providerid and PA."LocationId" = locationid and PA."SpecializationId" is null
	 AND PA."VisitTypeId" = visittypeid

) THEN  return query select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
where PA."ProviderId" = providerid and PA."LocationId" = locationid and PA."SpecializationId" is null
	 AND PA."VisitTypeId" = visittypeid; 
--END IF; 3
	
	
ELSEIF EXISTS (select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
where PA."LocationId" = locationid and PA."SpecializationId" is null and PA."ProviderId" is null
	 AND PA."VisitTypeId" = visittypeid 

) THEN  return query select PA."ProviderAvailabilityVisitTypeId", PA."VisitTypeId", CT."VisitorName",PA."Duration"
	FROM "ProviderAvailabilityVisitType" PA

	JOIN "VisitType" CT ON CT."VisitTypeId" = PA."VisitTypeId"
where PA."LocationId" = locationid and PA."SpecializationId" is null and PA."ProviderId" is null
	 AND PA."VisitTypeId" = visittypeid;
END IF; -- 1
end

$BODY$;

ALTER FUNCTION public."udf_provider_availability_visitType1"(integer, integer, integer, integer)
    OWNER TO postgres;
