<script lang="ts">
  import type {AccountOption} from "@pomade/core"
  import {Client} from "@pomade/core"
  import {uniqBy} from "@welshman/lib"
  import Button from "@lib/components2/Button.svelte"
  import Spinner from "@lib/components2/Spinner.svelte"
  import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
  import Icon from "@lib/components2/Icon.svelte"
  import Modal from "@lib/components2/Modal.svelte"
  import ModalBody from "@lib/components2/ModalBody.svelte"
  import ModalHeader from "@lib/components2/ModalHeader.svelte"
  import ModalTitle from "@lib/components2/ModalTitle.svelte"
  import ModalSubtitle from "@lib/components2/ModalSubtitle.svelte"
  import ModalFooter from "@lib/components2/ModalFooter.svelte"
  import Profile from "@app/components/Profile.svelte"
  import {deleteDeactivatedPomadeSessions, loginWithPomade} from "@app/pomade"
  import {getPomadeLoginFailureMessage, POMADE_NETWORK_ERROR_MESSAGE} from "@app/pomade"
  import {setChecked} from "@app/notifications"
  import {clearModals} from "@app/modal"
  import {pushToast} from "@app/toast"

  interface Props {
    email: string
    options: AccountOption[]
    clientSecret: string
  }

  const {email, options, clientSecret}: Props = $props()

  let loading = $state(false)

  const back = () => history.back()

  const selectAccount = async ({client, peers}: AccountOption) => {
    loading = true

    try {
      const {clientOptions, ...res} = await Client.selectLogin(clientSecret, client, peers)

      if (res.ok && clientOptions) {
        loginWithPomade(clientOptions, email)
        deleteDeactivatedPomadeSessions()
        setChecked("*")
        clearModals()
      } else {
        console.error(res.messages)

        pushToast({
          theme: "error",
          message: getPomadeLoginFailureMessage(res.messages),
        })
      }
    } catch (error) {
      console.error("Login error:", error)

      pushToast({
        theme: "error",
        message: POMADE_NETWORK_ERROR_MESSAGE,
      })
    } finally {
      loading = false
    }
  }
</script>

<Modal>
  <ModalBody>
    <ModalHeader>
      <ModalTitle>Select Account</ModalTitle>
      <ModalSubtitle
        >Multiple accounts are associated with {email}. Please select one to continue.</ModalSubtitle>
    </ModalHeader>
    <div class="flex flex-col gap-2">
      {#each uniqBy(o => o.pubkey, options) as option (option.pubkey)}
        <Button
          variant="secondary"
          onclick={() => selectAccount(option)}
          disabled={loading}
          class="flex w-full items-center p-3 text-left">
          <Profile inert pubkey={option.pubkey} />
        </Button>
      {/each}
    </div>
  </ModalBody>
  <ModalFooter>
    <Button variant="ghost" onclick={back} disabled={loading}>
      <Icon icon={AltArrowLeft} />
      Go back
    </Button>
    <Spinner {loading} />
  </ModalFooter>
</Modal>
