<template>

    <div class="notifyGP">

        <input type="hidden" v-model="question.answer" :name="questionName">

        <gp-practice-search
                search-url="/api/gp-addresses/search"
                search-placeholder="Search GP practice name or address"
                :user-practice="gpPractice"
                :instance-id="question.id"
                @address-updated="updateAnswer"
        ></gp-practice-search>

        <!-- COMMENTED OUT - Legacy DoctorSurgery component (to be removed after client validation)
        <gp-surgery-search
                search-url="/shop/ajax/doctor_surgery_search.php"
                search-placeholder="Search doctor surgery name or address"
                :user-surgery="surgery"
                :instance-id="question.id"
                @address-updated="updateAnswer"
        ></gp-surgery-search>
        -->

    </div>

</template>

<script>

import { mapGetters } from 'vuex';
import { props, computed, methods } from "../mixins/fields";
import { debounce } from 'lodash';

export default {
    name: 'notifyGp',
    mixins: [props, computed, methods],
    computed: {

        // GpAddresses package - use new GP practice getter
        ...mapGetters({
            gpPractice: 'getUserPractice'
        })

        // COMMENTED OUT - Legacy DoctorSurgery mapping (to be removed after client validation)
        // ...mapGetters({
        //     surgery: 'getUserSurgery'
        // }),
        //
        // gpPractice() {
        //     if (!this.surgery) {
        //         return {
        //             name: '',
        //             address_line_1: '',
        //             address_line_2: '',
        //             town: '',
        //             county: '',
        //             postcode: '',
        //             telephone_number: '',
        //             id: '',
        //             org_id: '',
        //             manual_gp_practice: false
        //         };
        //     }
        //     return {
        //         name: this.surgery.practice_name || this.surgery.name || '',
        //         address_line_1: this.surgery.address_1 || this.surgery.address_line_1 || '',
        //         address_line_2: this.surgery.address_2 || this.surgery.address_line_2 || '',
        //         town: this.surgery.city || this.surgery.town || '',
        //         county: this.surgery.address_3 || this.surgery.county || '',
        //         postcode: this.surgery.postcode || '',
        //         telephone_number: this.surgery.phone || this.surgery.telephone_number || '',
        //         id: this.surgery.id || '',
        //         org_id: this.surgery.ods_code || this.surgery.org_id || '',
        //         manual_gp_practice: this.surgery.manual_doctor_surgery || this.surgery.manual_gp_practice || false
        //     };
        // }

    },
    methods: {

        updateAnswer(address) {

            if(address) {
                this.question.answer = address;
            }else{
                this.question.answer = '';
            }

            this.updateQuestion(this.question);
        },

        updateQuestion: debounce(function(question) {

            question = this.validate(question);
            this.$emit('update-question', question);

        }, 200),

        validate(question) {

            let canProceed = true;

            if(typeof question.answer == 'object') {

                // Updated validation for new GP practice field names
                if((!question.answer?.address_line_1?.length && !question.answer?.town && question.answer?.postcode) && question.is_required) {
                    canProceed = false
                }
                // Legacy validation (for backwards compatibility during transition)
                if((!question.answer?.address_1?.length && !question.answer?.city && question.answer?.postcode) && question.is_required) {
                    canProceed = false
                }

            }else {
                if (!question.answer?.length && question.is_required) {
                    canProceed = false
                }
            }

            question.can_proceed = canProceed;

            return question;

        }


    }
}
</script>
