-- FetchDoctors
-- FUNCTION: mobile.FetchDoctors(text, integer)

-- DROP FUNCTION IF EXISTS mobile."FetchDoctors"(text, integer);

CREATE OR REPLACE FUNCTION mobile."FetchDoctors"(
	"searchText" text,
	"locationId" integer,
	"date" date)
    RETURNS TABLE("ProviderId" integer, "FullName" text, "Gender" character varying, "Experience" text, "ThumbnailUrl" text, "Languages" text, "Symptom" text, "ProviderAvailabilityId" int, "Guid" text) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	SELECT
	DISTINCT ON (B."ProviderId") 
		B."ProviderId",
		B."FullName",
		B."Gender"::character varying,
		B."Experience"::TEXT,
		B."ThumbnailUrl",
		B."Languages",
		e2."Symptom"::TEXT,
		e3."ProviderAvailabilityId",
		B."Guid"::TEXT
FROM
	(
	SELECT A
		."ProviderId",
		A."FullName",
		A."Gender",
		A."Experience",
		A."ThumbnailUrl",
		A."Languages",
		A."Guid" 
	FROM
		(
		SELECT P
			."ProviderId",
			P."FullName",
			P."Gender",
			P."Experience",
			P."ThumbnailUrl",
			P."Languages",
			P."Guid",
			UNNEST ( P."Specializations" ) "SpecializationId" 
		FROM
			"public"."Provider" P 
		)
		A
		join "Account" a2 on (a2."ReferenceId" = A."ProviderId" and  a2."RoleId" = 3)
		join "LocationAccountMap" lam on lam."AccountId" = a2."AccountId" 
		JOIN "public"."Specialization" s ON s."SpecializationId" = A."SpecializationId"
		LEFT JOIN mobile."ConsultLookupMaster" M ON M."SpecializationId" = s."SpecializationId"
		LEFT JOIN mobile."ConsultLookup" C ON C."ConsultLookupMasterId" = M."ConsultLookupMasterId"
	where lam."LocationId" = "locationId" and (
	A."FullName" ilike '%' || "searchText" || '%' OR M."Name" ilike '%' || "searchText" || '%' OR C."Name" ilike '%' || "searchText" || '%')
		
	) B
	LEFT JOIN LATERAL (
		SELECT
			json_agg ( json_build_object ( 'ConsultLookupMasterId', M."ConsultLookupMasterId", 'Name', M."Name" ) ) AS "Symptom" 
		FROM
			( SELECT P."ProviderId", UNNEST ( P."Specializations" ) "SpecializationId" FROM "public"."Provider" P WhERE P."ProviderId" = B."ProviderId" ) AA
			JOIN "public"."Specialization" s ON s."SpecializationId" = AA."SpecializationId"
			LEFT JOIN mobile."ConsultLookupMaster" M ON M."SpecializationId" = s."SpecializationId"
		WHERE
		AA."ProviderId" = B."ProviderId"
		LIMIT 1
	) e2 ON TRUE
	
	LEFT JOIN LATERAL (
		SELECT
			AA."ProviderAvailabilityId"
		FROM
			 "ProviderAvailability" AA
			JOIN "public"."Specialization" s ON s."SpecializationId" = AA."SpecializationId"
			LEFT JOIN mobile."ConsultLookupMaster" M ON M."SpecializationId" = s."SpecializationId"
		WHERE
			-- ConsultationTypeId 1 = Physycial
			AA."ProviderId" = B."ProviderId" 
			and AA."ConsultationTypeId" = 1
			and AA."StartDate" IS NOT NULL
			AND AA."EndDate" IS NOT NULL
			AND "date"::DATE >= AA."StartDate"::DATE
			AND "date"::DATE <= AA."EndDate"::DATE
		LIMIT 1
	) e3 ON TRUE

$BODY$;

ALTER FUNCTION mobile."FetchDoctors"(text, integer, date)
    OWNER TO postgres;


-- FetchConsultItems
-- FUNCTION: mobile.FetchConsultItems(boolean)

-- DROP FUNCTION IF EXISTS mobile."FetchConsultItems"(boolean);

CREATE OR REPLACE FUNCTION "mobile"."FetchConsultItems"("mainPageItems" bool)
  RETURNS TABLE("ConsultLookupId" int4, "ConsultLookupMasterId" int4, "MasterName" varchar, "Name" varchar, "Image" text, "ShowOnMainPage" bool) AS $BODY$
 
	
	SELECT
	c."ConsultLookupId",
	m."ConsultLookupMasterId",
	m."Name" "MasterName",
	c."Name",
	c."Image",
	c."ShowOnMainPage"
FROM
	"mobile"."ConsultLookup" c
	JOIN "mobile"."ConsultLookupMaster" m on m."ConsultLookupMasterId" = c."ConsultLookupMasterId"
WHERE
	CASE WHEN "mainPageItems" IS FALSE THEN c."ShowOnMainPage" IS NULL ELSE (CASE WHEN "mainPageItems" IS NULL THEN c."ShowOnMainPage" IS NULL OR c."ShowOnMainPage" IS TRUE ELSE c."ShowOnMainPage" IS TRUE END) END
	
	
$BODY$
  LANGUAGE sql VOLATILE
  COST 100
  ROWS 1000;

-- GetAccountByUuid
CREATE OR REPLACE FUNCTION mobile."GetAccountByUuid"(
	uuid text)
    RETURNS TABLE("AccountId" integer, "RoleId" integer, "ReferenceId" integer, "Email" character varying, "Mobile" character varying, "CountryId" integer, "FullName" text, "UserName" character varying, "Active" boolean) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
	SELECT A
	."AccountId",
	"RoleId",
	"ReferenceId",
	a."Email",
	a."Mobile",
	"CountryId",
	"FullName",
	"UserName",
	"Active" 
FROM
	"public"."Account"
	A LEFT JOIN "mobile"."MobileAccount" f ON f."AccountId" = A."AccountId" 
WHERE
	f."Uuid" = "uuid" And "RoleId" = 4
	
	
$BODY$;

ALTER FUNCTION mobile."GetAccountByUuid"(text)
    OWNER TO postgres;
-- FUNCTION: mobile.GetAccountByMobile(text)

-- DROP FUNCTION IF EXISTS mobile."GetAccountByMobile"(text);

CREATE OR REPLACE FUNCTION mobile."GetAccountByMobile"(
	mobile text)
    RETURNS TABLE("AccountId" integer, "RoleId" integer, "ReferenceId" integer, "Email" character varying, "Mobile" character varying, "CountryId" integer, "FullName" text, "UserName" character varying, "Active" boolean) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
	SELECT A
	."AccountId",
	"RoleId",
	"ReferenceId",
	"Email",
	"Mobile",
	"CountryId",
	"FullName",
	"UserName",
	"Active" 
FROM
	"Account" A
WHERE
	A."Mobile" = "mobile" And "RoleId" = 4
	
	
$BODY$;

ALTER FUNCTION mobile."GetAccountByMobile"(text)
    OWNER TO postgres;

-- FUNCTION: mobile.GetLastUmrNo()

-- DROP FUNCTION IF EXISTS mobile."GetLastUmrNo"();

CREATE OR REPLACE FUNCTION mobile."GetLastUmrNo"(
	)
    RETURNS TABLE("UmrNo" text) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
SELECT "UMRNo" "UmrNo" FROM "Patient" WHERE "UMRNo" ilike '%UMR%' ORDER BY "PatientId" DESC LIMIT 1
	
	
$BODY$;

ALTER FUNCTION mobile."GetLastUmrNo"()
    OWNER TO postgres;

-- FUNCTION: mobile.FetchAppointments(integer, date)

-- DROP FUNCTION IF EXISTS mobile."FetchAppointments"(integer, date);

CREATE OR REPLACE FUNCTION mobile."FetchAppointments"(
	"patientId" integer,
	"date" date default null)
    RETURNS TABLE("AppointmentId" integer, "DoctorName" character varying, "DoctorGender" character, "ThumbnailUrl" text, "SpecializationName" character varying, "AppointmentNo" character varying, "AppointmentDate" date, "AppointmentTime" time without time zone, "AppointmentEndTime" time without time zone, "Guid" text, "TokenNumber" integer, "QueueStatusId" integer) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
	
	select 
	a."AppointmentId",
	p."FullName" "DoctorName",
	p."Gender" "DoctorGender",
	P."ThumbnailUrl",
	s."SpecializationName",
	a."AppointmentNo",
	a."AppointmentDate",
	a."AppointmentTime",
	a."AppointmentEndTime",
	p."Guid"::TEXT,
	a."TokenNumber",
	a."QueueStatusId"
	from "Appointment" a
	JOIN "Provider" p on p."ProviderId" = a."ProviderId"
	JOIN "Specialization" s on s."SpecializationId" = a."SpecializationId"
	WHERE a."PatientId" = "patientId" AND CASE
	WHEN "date" IS NOT NULL THEN
		a."AppointmentDate" = "date"::DATE
	ELSE
		1 = 1 END;
	
$BODY$;

ALTER FUNCTION mobile."FetchAppointments"(integer, date)
    OWNER TO postgres;

-- FUNCTION: mobile.GetAccountByMobile(text)

-- DROP FUNCTION IF EXISTS mobile."GetAccountByMobile"(text);

CREATE OR REPLACE FUNCTION mobile."GetAccountByMobile"(
	mobile text)
    RETURNS TABLE("AccountId" integer, "RoleId" integer, "ReferenceId" integer, "Email" character varying, "Mobile" character varying, "CountryId" integer, "FullName" text, "UserName" character varying, "Active" boolean) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
	SELECT A
	."AccountId",
	"RoleId",
	"ReferenceId",
	"Email",
	"Mobile",
	"CountryId",
	"FullName",
	"UserName",
	"Active" 
FROM
	"Account" A
WHERE
	A."Mobile" = "mobile" And "RoleId" = 4
	
	
$BODY$;

ALTER FUNCTION mobile."GetAccountByMobile"(text)
    OWNER TO postgres;

-- FUNCTION: mobile.GetAccountByMobile(text)

-- DROP FUNCTION IF EXISTS mobile."GetAccountByMobile"(text);

CREATE OR REPLACE FUNCTION mobile."GetAccountByMobile"(
	mobile text)
    RETURNS TABLE("AccountId" integer, "RoleId" integer, "ReferenceId" integer, "Email" character varying, "Mobile" character varying, "CountryId" integer, "FullName" text, "UserName" character varying, "Active" boolean) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
	SELECT A
	."AccountId",
	"RoleId",
	"ReferenceId",
	"Email",
	"Mobile",
	"CountryId",
	"FullName",
	"UserName",
	"Active" 
FROM
	"Account" A
WHERE
	A."Mobile" = "mobile" And "RoleId" = 4
	
	
$BODY$;

ALTER FUNCTION mobile."GetAccountByMobile"(text)
    OWNER TO postgres;

-- FUNCTION: mobile.GetAccountByMobile(text, text)

-- DROP FUNCTION IF EXISTS mobile."GetAccountByMobile"(text, text);

CREATE OR REPLACE FUNCTION mobile."GetAccountByMobile"(
	"countryCode" text,
	mobile text)
    RETURNS TABLE("AccountId" integer, "RoleId" integer, "ReferenceId" integer, "Email" character varying, "Mobile" character varying, "CountryId" integer, "FullName" text, "UserName" character varying, "Active" boolean) 
    LANGUAGE 'sql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
 
	
	SELECT A
	."AccountId",
	"RoleId",
	"ReferenceId",
	"Email",
	"Mobile",
	a."CountryId",
	"FullName",
	"UserName",
	a."Active" 
FROM
	"Account" A
JOIN "Country" c on c."CountryId" = a."CountryId"
WHERE
	A."Mobile" = "mobile" and c."CountryCode" = "countryCode" And "RoleId" = 4
	
	
$BODY$;

ALTER FUNCTION mobile."GetAccountByMobile"(text, text)
    OWNER TO postgres;



