<script lang="ts">
  import {getTag, ManagementMethod} from "@welshman/util"
  import type {TrustedEvent} from "@welshman/util"
  import {manageRelay, repository, displayProfileByPubkey} from "@welshman/app"
  import InboxOut from "@assets/icons/inbox-out.svg?dataurl"
  import MenuDots from "@assets/icons/menu-dots.svg?dataurl"
  import MinusCircle from "@assets/icons/minus-circle.svg?dataurl"
  import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
  import {fly} from "@lib/transition"
  import Icon from "@lib/components/Icon.svelte"
  import Popover from "@lib/components/Popover.svelte"
  import Button from "@lib/components/Button.svelte"
  import Confirm from "@lib/components/Confirm.svelte"
  import {pushToast} from "@app/util/toast"
  import {pushModal} from "@app/util/modal"

  type Props = {
    url: string
    event: TrustedEvent
  }

  const {url, event}: Props = $props()

  const etag = getTag("e", event.tags)
  const ptag = getTag("p", event.tags)

  const toggleMenu = () => {
    isOpen = !isOpen
  }

  const closeMenu = () => {
    isOpen = false
  }

  const dismissReport = async () => {
    const {error} = await manageRelay(url, {
      method: ManagementMethod.BanEvent,
      params: [event.id, "Dismissed by admin"],
    })

    if (error) {
      pushToast({theme: "error", message: error})
    } else {
      pushToast({message: "Content has successfully been deleted!"})
      repository.removeEvent(event.id)
      history.back()
    }
  }

  const banContent = () => {
    const [_, id, reason = ""] = etag!

    pushModal(Confirm, {
      title: `Delete Content`,
      message: `Are you sure you want to delete this content from the space?`,
      confirm: async () => {
        const {error} = await manageRelay(url, {
          method: ManagementMethod.BanEvent,
          params: [id, reason],
        })

        if (error) {
          pushToast({theme: "error", message: error})
        } else {
          pushToast({message: "Content has successfully been deleted!"})
          repository.removeEvent(id)
          history.back()
        }
      },
    })
  }

  const banMember = () => {
    const [pubkey, reason = ""] = ptag!

    pushModal(Confirm, {
      title: "Ban User",
      message: `Are you sure you want to ban @${displayProfileByPubkey(pubkey)} from the space?`,
      confirm: async () => {
        const {error} = await manageRelay(url, {
          method: ManagementMethod.BanPubkey,
          params: [pubkey, reason],
        })

        if (error) {
          pushToast({theme: "error", message: error})
        } else {
          pushToast({message: "User has successfully been banned!"})
          history.back()
        }
      },
    })
  }

  let isOpen = $state(false)
</script>

<div class="relative">
  <Button class="btn btn-circle btn-ghost btn-sm" onclick={toggleMenu}>
    <Icon icon={MenuDots} />
  </Button>
  {#if isOpen}
    <Popover hideOnClick onClose={closeMenu}>
      <ul
        transition:fly
        class="menu absolute right-0 z-popover mt-2 w-48 gap-1 rounded-box bg-base-100 p-2 shadow-md">
        <li>
          <Button onclick={dismissReport}>
            <Icon icon={InboxOut} />
            Dismiss Report
          </Button>
        </li>
        {#if etag}
          <li>
            <Button class="text-error" onclick={banContent}>
              <Icon icon={TrashBin2} />
              Remove Content
            </Button>
          </li>
        {/if}
        {#if ptag}
          <li>
            <Button class="text-error" onclick={banMember}>
              <Icon icon={MinusCircle} />
              Ban User
            </Button>
          </li>
        {/if}
      </ul>
    </Popover>
  {/if}
</div>
