create policy "doctor_select"
on "medreport"."analysis_responses"
as PERMISSIVE
for SELECT
to authenticated
using (medreport.is_doctor());

create policy "doctor_select"
on "medreport"."analysis_response_elements"
as PERMISSIVE
for SELECT
to authenticated
using (medreport.is_doctor());

create policy "doctor_select"
on "medreport"."accounts"
as PERMISSIVE
for SELECT
to authenticated
using (medreport.is_doctor());

create type medreport.analysis_feedback_status as enum ('STARTED', 'DRAFT', 'COMPLETED');

CREATE TABLE medreport.doctor_analysis_feedback (
    id BIGINT GENERATED BY DEFAULT as IDENTITY NOT NULL,
    analysis_order_id BIGINT NOT NULL REFERENCES medreport.analysis_orders(id),
    doctor_user_id UUID NULL REFERENCES auth.users(id),
    user_id UUID NOT NULL REFERENCES auth.users(id),
    value TEXT,
    status medreport.analysis_feedback_status DEFAULT 'STARTED' NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    created_by UUID DEFAULT auth.uid() REFERENCES auth.users(id) NOT NULL,
    updated_by UUID DEFAULT auth.uid() REFERENCES auth.users(id),
    PRIMARY KEY (id)
);

CREATE UNIQUE INDEX doctor_analysis_feedback_analysis_order_id_key ON medreport.doctor_analysis_feedback USING btree (analysis_order_id);

CREATE OR REPLACE FUNCTION medreport.update_doctor_analysis_feedback_updated_fields()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    NEW.updated_by = auth.uid();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER trigger_update_doctor_analysis_feedback_updated_fields
    AFTER UPDATE ON medreport.doctor_analysis_feedback
    FOR EACH ROW
    EXECUTE FUNCTION medreport.update_doctor_analysis_feedback_updated_fields();

CREATE OR REPLACE FUNCTION medreport.set_doctor_analysis_feedback_created_fields()
RETURNS TRIGGER AS $$
BEGIN
    NEW.created_at = NOW();
    NEW.created_by = auth.uid();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

CREATE TRIGGER trigger_insert_doctor_analysis_feedback_created_fields
    AFTER INSERT ON medreport.doctor_analysis_feedback
    FOR EACH ROW
    EXECUTE FUNCTION medreport.set_doctor_analysis_feedback_created_fields();

CREATE INDEX idx_doctor_analysis_feedback_analysis_order_id ON medreport.doctor_analysis_feedback(analysis_order_id);
CREATE INDEX idx_doctor_analysis_feedback_doctor_user_id ON medreport.doctor_analysis_feedback(doctor_user_id);

grant select,insert, update, delete on table medreport.doctor_analysis_feedback to authenticated;

ALTER TABLE medreport.doctor_analysis_feedback ENABLE ROW LEVEL SECURITY;

create policy "user_select"
on "medreport"."doctor_analysis_feedback"
as PERMISSIVE
for SELECT
to authenticated
using (auth.uid() = user_id);

create policy "doctor_all"
on "medreport"."doctor_analysis_feedback"
as PERMISSIVE
for ALL
to authenticated
using (medreport.is_doctor());

create policy "doctor_select"
on "medreport"."account_params"
as PERMISSIVE
for SELECT
to authenticated
using (medreport.is_doctor());

