<!--
     This file is part of the happyDomain (R) project.
     Copyright (c) 2022-2024 happyDomain
     Authors: Pierre-Olivier Mercier, et al.

     This program is offered under a commercial and under the AGPL license.
     For commercial licensing, contact us at <contact@happydomain.org>.

     For AGPL licensing:
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as published by
     the Free Software Foundation, either version 3 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->

<script lang="ts">
    import { Alert, Button, FormGroup, Icon, Input } from "@sveltestrap/sveltestrap";

    import type { dnsResource } from "$lib/dns_rr";
    import CAAIssuer from "./CAA-issuer.svelte";
    import CAAIodef from "./CAA-iodef.svelte";
    import type { Domain } from "$lib/model/domain";
    import { t } from "$lib/translations";
    import { CAAPolicy, newCAARecord, type CAATag } from "$lib/services/caa.svelte";

    interface Props {
        dn: string;
        origin: Domain;
        readonly?: boolean;
        value: dnsResource;
    }

    let { dn, origin, readonly = false, value = $bindable({}) }: Props = $props();

    function addIssuer(tag: CAATag): (e: CustomEvent<string>) => void {
        return (e: CustomEvent<string>) => {
            if (!value["caa"]) value["caa"] = [];
            if (!Array.isArray(value["caa"])) value["caa"] = [value["caa"]];
            value["caa"].push(newCAARecord(dn, tag, e.detail));
        };
    }

    let val = $derived(new CAAPolicy(value));

    const type = "svcs.CAAPolicy";
</script>

<h4 class="mt-4">{$t("resources.CAA.title")}</h4>

<FormGroup>
    <Input
        id="issuedisabled"
        type="checkbox"
        label={$t("resources.CAA.no-issuers-hint")}
        checked={val.DisallowIssue}
        on:change={val.changeDisallowIssue(dn, "issue")}
    />
</FormGroup>

<h5>
    {$t("resources.CAA.auth-issuers")}
</h5>

{#if !val.DisallowIssue}
    <ul>
        {#if val.records.filter((r) => r.Tag == "issue").length}
            {#each val.records as issue, k}
                {#if issue.Tag == "issue"}
                    <li class="mb-3">
                        <CAAIssuer
                            {readonly}
                            bind:flag={val.records[k].Flag}
                            bind:tag={val.records[k].Tag}
                            bind:value={val.records[k].Value}
                            on:delete-issuer={() => {
                                val.records.splice(k, 1);
                            }}
                        />
                    </li>
                {/if}
            {/each}
        {:else}
            <Alert color="warning" fade={false}>
                <strong>{$t("resources.CAA.all-issuers-title")}</strong>
                {$t("resources.CAA.all-issuers-body")}
            </Alert>
        {/if}
        {#if !readonly}
            <li style:list-style="'+ '">
                <CAAIssuer newone on:add-issuer={addIssuer("issue")} />
            </li>
        {/if}
    </ul>
{:else}
    <Alert color="danger" fade={false}>
        <strong>{$t("resources.CAA.no-issuers-title")}</strong>
        {$t("resources.CAA.no-issuers-body")}
    </Alert>
{/if}

<h4 class="mt-4">{$t("resources.CAA.wild-issuers")}</h4>

<FormGroup>
    <Input
        id="wildcardissuedisabled"
        type="checkbox"
        label={$t("resources.CAA.no-wild-hint")}
        checked={val.DisallowWildcardIssue}
        on:change={val.changeDisallowIssue(dn, "issuewild")}
    />
</FormGroup>

<h5>
    {$t("resources.CAA.auth-issuers")}
</h5>

{#if !val.DisallowWildcardIssue}
    <ul>
        {#if val.records.filter((r) => r.Tag == "issuewild").length}
            {#each val.records as issue, k}
                {#if issue.Tag == "issuewild"}
                    <li class="mb-3">
                        <CAAIssuer
                            {readonly}
                            bind:flag={val.records[k].Flag}
                            bind:tag={val.records[k].Tag}
                            bind:value={val.records[k].Value}
                            on:delete-issuer={() => {
                                val.records.splice(k, 1);
                            }}
                        />
                    </li>
                {/if}
            {/each}
        {:else if val.DisallowIssue}
            <Alert color="danger" fade={false}>
                <strong>{$t("resources.CAA.no-issuers-title")}</strong>
                {$t("resources.CAA.no-wild-body")}
            </Alert>
        {:else if val.records.filter((r) => r.Tag == "issue").length}
            <Alert color="warning" fade={false}>
                <strong>{$t("resources.CAA.wild-same-title")}</strong>
                {$t("resources.CAA.wild-same-body")}
            </Alert>
        {:else}
            <Alert color="warning" fade={false}>
                <strong>{$t("resources.CAA.all-issuers-title")}</strong>
                {$t("resources.CAA.all-wild-issuers-body")}
            </Alert>
        {/if}
        {#if !readonly}
            <li style:list-style="'+ '">
                <CAAIssuer newone on:add-issuer={addIssuer("issuewild")} />
            </li>
        {/if}
    </ul>
{:else}
    <Alert color="danger" fade={false}>
        <strong>{$t("resources.CAA.no-wild-title")}</strong>
        {$t("resources.CAA.no-wild-body")}
    </Alert>
{/if}

<h4 class="mt-4">{$t("resources.CAA.mail-issuers")}</h4>

<FormGroup>
    <Input
        id="mailissuedisabled"
        type="checkbox"
        label={$t("resources.CAA.no-mail-hint")}
        checked={val.DisallowMailIssue}
        on:change={val.changeDisallowIssue(dn, "issuemail")}
    />
</FormGroup>

{#if !val.DisallowMailIssue && !val.records.filter((r) => r.Tag == "issuemail").length}
    <Alert color="warning" fade={false}>
        <strong>{$t("resources.CAA.mail-all-allowed-title")}</strong>
        {$t("resources.CAA.mail-all-allowed-body")}
    </Alert>
{/if}

<h5>
    {$t("resources.CAA.auth-issuers")}
</h5>

{#if !val.DisallowMailIssue}
    <ul>
        {#if val.records.filter((r) => r.Tag == "issuemail").length}
            {#each val.records as issue, k}
                {#if issue.Tag == "issuemail"}
                    <li class="mb-3">
                        <CAAIssuer
                            {readonly}
                            bind:flag={val.records[k].Flag}
                            bind:tag={val.records[k].Tag}
                            bind:value={val.records[k].Value}
                            on:delete-issuer={() => {
                                val.records.splice(k, 1);
                            }}
                        />
                    </li>
                {/if}
            {/each}
        {/if}
        {#if !readonly}
            <li style:list-style="'+ '">
                <CAAIssuer newone on:add-issuer={addIssuer("issuemail")} />
            </li>
        {/if}
    </ul>
{:else}
    <Alert color="danger" fade={false}>
        <strong>{$t("resources.CAA.no-mail-title")}</strong>
        {$t("resources.CAA.no-mail-body")}
    </Alert>
{/if}

<h4 class="mt-4">{$t("resources.CAA.vmc-issuers")}</h4>

<FormGroup>
    <Input
        id="vmcissuedisabled"
        type="checkbox"
        label={$t("resources.CAA.no-vmc-hint")}
        checked={val.DisallowVMCIssue}
        on:change={val.changeDisallowIssue(dn, "issuevmc")}
    />
</FormGroup>

{#if !val.DisallowVMCIssue && !val.records.filter((r) => r.Tag == "issuevmc").length}
    <Alert color="warning" fade={false}>
        <strong>{$t("resources.CAA.vmc-all-allowed-title")}</strong>
        {$t("resources.CAA.vmc-all-allowed-body")}
    </Alert>
{/if}

<h5>
    {$t("resources.CAA.auth-issuers")}
</h5>

{#if !val.DisallowVMCIssue}
    <ul>
        {#if val.records.filter((r) => r.Tag == "issuevmc").length}
            {#each val.records as issue, k}
                {#if issue.Tag == "issuevmc"}
                    <li class="mb-3">
                        <CAAIssuer
                            {readonly}
                            bind:flag={val.records[k].Flag}
                            bind:tag={val.records[k].Tag}
                            bind:value={val.records[k].Value}
                            on:delete-issuer={() => {
                                val.records.splice(k, 1);
                            }}
                        />
                    </li>
                {/if}
            {/each}
        {/if}
        {#if !readonly}
            <li style:list-style="'+ '">
                <CAAIssuer newone on:add-issuer={addIssuer("issuevmc")} />
            </li>
        {/if}
    </ul>
{:else}
    <Alert color="danger" fade={false}>
        <strong>{$t("resources.CAA.no-vmc-title")}</strong>
        {$t("resources.CAA.no-vmc-body")}
    </Alert>
{/if}

<h4 class="mt-4">{$t("resources.CAA.incident-response")}</h4>

<p>
    {$t("resources.CAA.incident-response-text")}
</p>

{#if val.records.filter((r) => r.Tag == "iodef").length}
    {#each val.records as issue, k}
        {#if issue.Tag == "iodef"}
            <CAAIodef
                {readonly}
                bind:flag={val.records[k].Flag}
                bind:tag={val.records[k].Tag}
                bind:value={val.records[k].Value}
                on:delete-iodef={() => {
                    val.records.splice(k, 1);
                }}
            />
        {/if}
    {/each}
{/if}
{#if !readonly}
    <CAAIodef newone on:add-iodef={addIssuer("iodef")} />
{/if}

<h4 class="mt-4">{$t("resources.CAA.contact-info")}</h4>

<p>
    {$t("resources.CAA.contact-info-text")}
</p>

<h5>{$t("resources.CAA.contact-email")}</h5>

{#if val.records.filter((r) => r.Tag == "contactemail").length}
    <ul>
        {#each val.records as contact, k}
            {#if contact.Tag == "contactemail"}
                <li class="mb-2">
                    <div class="d-flex align-items-center gap-2">
                        <Input
                            type="email"
                            bind:value={val.records[k].Value}
                            {readonly}
                            placeholder="contact@example.com"
                        />
                        {#if !readonly}
                            <Button
                                type="button"
                                size="sm"
                                color="danger"
                                on:click={() => {
                                    val.records.splice(k, 1);
                                }}
                            >
                                <Icon name="trash" />
                            </Button>
                        {/if}
                    </div>
                </li>
            {/if}
        {/each}
    </ul>
{/if}
{#if !readonly}
    <Button
        type="button"
        size="sm"
        color="primary"
        outline
        on:click={() => {
            if (!value["caa"]) value["caa"] = [];
            if (!Array.isArray(value["caa"])) value["caa"] = [value["caa"]];
            value["caa"].push(newCAARecord(dn, "contactemail", ""));
        }}
    >
        <Icon name="plus" />
        {$t("resources.CAA.add-contact-email")}
    </Button>
{/if}

<h5 class="mt-3">{$t("resources.CAA.contact-phone")}</h5>

{#if val.records.filter((r) => r.Tag == "contactphone").length}
    <ul>
        {#each val.records as contact, k}
            {#if contact.Tag == "contactphone"}
                <li class="mb-2">
                    <div class="d-flex align-items-center gap-2">
                        <Input
                            type="tel"
                            bind:value={val.records[k].Value}
                            {readonly}
                            placeholder="+1-555-0123"
                        />
                        {#if !readonly}
                            <Button
                                type="button"
                                size="sm"
                                color="danger"
                                on:click={() => {
                                    val.records.splice(k, 1);
                                }}
                            >
                                <Icon name="trash" />
                            </Button>
                        {/if}
                    </div>
                </li>
            {/if}
        {/each}
    </ul>
{/if}
{#if !readonly}
    <Button
        type="button"
        size="sm"
        color="primary"
        outline
        on:click={() => {
            if (!value["caa"]) value["caa"] = [];
            if (!Array.isArray(value["caa"])) value["caa"] = [value["caa"]];
            value["caa"].push(newCAARecord(dn, "contactphone", ""));
        }}
    >
        <Icon name="plus" />
        {$t("resources.CAA.add-contact-phone")}
    </Button>
{/if}
