<template>
    <card-page
        :title="$gettext('SoundExchange Report')"
    >
        <form
            id="report-form"
            class="form vue-form"
            method="GET"
            :action="apiUrl"
            target="_blank"
        >
            <div class="card-body">
                <form-fieldset>
                    <p>
                        This report is intended for licensing in the United States only, for webcasters paying royalties
                        via SoundExchange. Learn more about the requirements for reporting and classification on the
                        <a
                            href="https://www.soundexchange.com/service-provider/reporting-requirements/"
                            target="_blank"
                        >
                            SoundExchange web site
                        </a>.
                    </p>

                    <ul>
                        <li>
                            AzuraCast assumes that your station fits SoundExchange Transmission Category A, "Eligible
                            nonsubscription transmissions other than broadcast simulcasts and transmissions of non-music
                            programming." If your station does not fall within this category, update the transmission
                            category field accordingly.
                        </li>
                        <li>
                            The data collected by AzuraCast meets the SoundExchange standard for Actual Total
                            Performances (ATP) by tracking unique listeners across all song plays. All other information
                            is derived from the metadata of the uploaded songs themselves, and may not be completely
                            accurate.
                        </li>
                        <li>
                            Reporting requirements for SoundExchange may change at any time. AzuraCast is non-commercial
                            community-built software and cannot guarantee that its reporting format will always be
                            up-to-date.
                        </li>
                        <li>
                            You should always verify the report generated by AzuraCast before sending it. In particular,
                            either the ISRC (International Standard Recording Code) or *both* the album and label are
                            required for every row, and may not be provided in the song metadata. To locate an ISRC for
                            a track, you should use <a
                                href="https://isrc.soundexchange.com"
                                target="_blank"
                            >SoundExchange's
                                ISRC search tool</a>.
                        </li>
                    </ul>
                </form-fieldset>

                <form-fieldset class="row">
                    <form-group-field
                        id="form_start_date"
                        name="start_date"
                        :field="v$.start_date"
                        input-type="date"
                        class="col mb-3"
                    >
                        <template #label>
                            {{ $gettext('Start Date') }}
                        </template>
                    </form-group-field>

                    <form-group-field
                        id="form_end_date"
                        name="end_date"
                        :field="v$.end_date"
                        input-type="date"
                        class="col mb-3"
                    >
                        <template #label>
                            {{ $gettext('End Date') }}
                        </template>
                    </form-group-field>

                    <form-group-checkbox
                        id="form_edit_fetch_isrc"
                        name="fetch_isrc"
                        :field="v$.fetch_isrc"
                        class="col-md-12"
                    >
                        <template #label>
                            {{ $gettext('Attempt to Automatically Retrieve ISRC When Missing') }}
                        </template>
                        <template #description>
                            {{
                                $gettext('If enabled, AzuraCast will connect to the MusicBrainz database to attempt to find an ISRC for any files where one is missing. Disabling this may improve performance.')
                            }}
                        </template>
                    </form-group-checkbox>
                </form-fieldset>

                <button
                    type="submit"
                    class="btn"
                    :class="(v$.$invalid) ? 'btn-danger' : 'btn-primary'"
                >
                    {{ $gettext('Generate Report') }}
                </button>
            </div>
        </form>
    </card-page>
</template>

<script setup lang="ts">
import {required} from '@vuelidate/validators';
import FormGroupField from "~/components/Form/FormGroupField.vue";
import FormFieldset from "~/components/Form/FormFieldset.vue";
import FormGroupCheckbox from "~/components/Form/FormGroupCheckbox.vue";
import {useVuelidateOnForm} from "~/functions/useVuelidateOnForm";
import {getStationApiUrl} from "~/router";
import CardPage from "~/components/Common/CardPage.vue";
import useStationDateTimeFormatter from "~/functions/useStationDateTimeFormatter.ts";

const apiUrl = getStationApiUrl('/reports/soundexchange');

const {now} = useStationDateTimeFormatter();

const lastMonth = now().minus({months: 1});

const {v$} = useVuelidateOnForm(
    {
        start_date: {required},
        end_date: {required},
        fetch_isrc: {}
    },
    {
        start_date: lastMonth.startOf('month').toISODate(),
        end_date: lastMonth.endOf('month').toISODate(),
        fetch_isrc: false
    }
);
</script>
