<script lang="ts">
  import { _ } from "svelte-i18n";
  import { invalidateAll } from "$app/navigation";
  import { untrack } from "svelte";
  import { getAssetUrl } from "$lib/api";
  import { toast } from "svelte-sonner";
  import { updateProfile, uploadFile, removeFile } from "$lib/services";
  import { Button } from "$lib/components/ui/button";
  import { Input } from "$lib/components/ui/input";
  import { Label } from "$lib/components/ui/label";
  import { Textarea } from "$lib/components/ui/textarea";
  import { TagsInput } from "$lib/components/ui/tags-input";
  import { FileDropZone, MEGABYTE } from "$lib/components/ui/file-drop-zone";
  import * as Alert from "$lib/components/ui/alert";
  import { Card, CardContent } from "$lib/components/ui/card";
  import Meta from "$lib/components/meta/meta.svelte";

  const { data } = $props();

  let firstName = $state(untrack(() => data.authStatus.user!.first_name));
  let lastName = $state(untrack(() => data.authStatus.user!.last_name));
  let artistName = $state(untrack(() => data.authStatus.user!.artist_name));
  let description = $state(untrack(() => data.authStatus.user!.description));
  let tags = $state(untrack(() => data.authStatus.user!.tags ?? undefined));

  $effect(() => {
    firstName = data.authStatus.user!.first_name;
    lastName = data.authStatus.user!.last_name;
    artistName = data.authStatus.user!.artist_name;
    description = data.authStatus.user!.description;
    tags = data.authStatus.user!.tags ?? undefined;
  });

  let isProfileLoading = $state(false);
  let isProfileError = $state(false);
  let profileError = $state("");

  let avatar = $state<{
    id?: string;
    url: string;
    name: string;
    size: number;
    file?: File;
  }>();

  function setExistingAvatar() {
    if (data.authStatus.user!.avatar) {
      avatar = {
        id: data.authStatus.user!.avatar,
        url: getAssetUrl(data.authStatus.user!.avatar, "thumbnail")!,
        name: data.authStatus.user!.artist_name ?? "",
        size: 0,
      };
    } else {
      avatar = undefined;
    }
  }

  $effect(() => {
    setExistingAvatar();
  });

  async function handleFilesUpload(files: File[]) {
    const file = files[0];
    avatar = {
      name: file.name,
      size: file.size,
      url: URL.createObjectURL(file),
      file,
    };
  }

  async function handleAvatarRemove() {
    if (avatar!.id) {
      avatar = undefined;
    } else {
      setExistingAvatar();
    }
  }

  async function handleProfileSubmit(e: Event) {
    e.preventDefault();
    try {
      isProfileLoading = true;
      isProfileError = false;
      profileError = "";

      let avatarId: string | null | undefined = undefined;

      if (!avatar?.id && data.authStatus.user!.avatar) {
        await removeFile(data.authStatus.user!.avatar);
        avatarId = null;
      } else if (avatar?.id) {
        avatarId = avatar.id;
      }

      if (avatar?.file) {
        const formData = new FormData();
        formData.append("file", avatar.file);
        const result = await uploadFile(formData);
        avatarId = result.id;
      }

      await updateProfile({
        first_name: firstName,
        last_name: lastName,
        artist_name: artistName,
        description,
        tags,
        avatar: avatarId ?? undefined,
      });
      toast.success($_("me.settings.toast_update"));
      invalidateAll();
    } catch (err) {
      const e = err as { response?: { errors?: Array<{ message: string }> }; message?: string };
      profileError = e.response?.errors?.[0]?.message ?? e.message ?? "Unknown error";
      isProfileError = true;
    } finally {
      isProfileLoading = false;
    }
  }
</script>

<Meta title={$_("me.settings.profile_title")} />

<div class="py-3 sm:py-6 lg:pl-6">
  <div class="mb-6">
    <h1 class="text-2xl font-bold">{$_("me.settings.profile_title")}</h1>
    <p class="text-sm text-muted-foreground mt-1">{$_("me.settings.profile_subtitle")}</p>
  </div>

  <Card class="bg-card/50 border-primary/20 max-w-2xl">
    <CardContent class="space-y-4 pt-6">
      <form onsubmit={handleProfileSubmit} class="space-y-4">
        <div class="space-y-2">
          <Label>{$_("me.settings.avatar")}</Label>
          <div class="flex items-center gap-5">
            <FileDropZone
              id="avatar"
              fileCount={0}
              maxFiles={1}
              maxFileSize={2 * MEGABYTE}
              onUpload={handleFilesUpload}
              accept="image/*"
              class="h-auto w-auto shrink-0 border-none p-0 rounded-full hover:bg-transparent"
            >
              <div class="relative group cursor-pointer w-24 h-24">
                {#if avatar}
                  <img
                    src={avatar.url}
                    alt={avatar.name}
                    class="w-24 h-24 rounded-full object-cover ring-4 ring-primary/20 group-hover:ring-primary/50 transition-all"
                  />
                  <div
                    class="absolute inset-0 rounded-full bg-black/50 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center"
                  >
                    <span class="icon-[ri--camera-line] w-7 h-7 text-white"></span>
                  </div>
                {:else}
                  <div
                    class="w-24 h-24 rounded-full border-2 border-dashed border-primary/30 group-hover:border-primary/60 bg-primary/5 group-hover:bg-primary/10 transition-all flex flex-col items-center justify-center gap-1"
                  >
                    <span
                      class="icon-[ri--camera-line] w-7 h-7 text-primary/50 group-hover:text-primary/80 transition-colors"
                    ></span>
                    <span class="text-xs text-muted-foreground">Upload</span>
                  </div>
                {/if}
              </div>
            </FileDropZone>
            <div class="flex flex-col gap-1">
              <p class="text-sm text-muted-foreground">JPG, PNG · max 2 MB</p>
              <p class="text-xs text-muted-foreground/70">
                Click or drop to {avatar ? "change" : "upload"}
              </p>
              {#if avatar}
                <Button
                  variant="ghost"
                  size="sm"
                  onclick={handleAvatarRemove}
                  class="cursor-pointer w-fit mt-1 px-2 h-7 text-xs text-muted-foreground hover:text-destructive hover:bg-destructive/10"
                >
                  <span class="icon-[ri--delete-bin-line] w-3.5 h-3.5 mr-1"></span>
                  Remove
                </Button>
              {/if}
            </div>
          </div>
        </div>

        <div class="grid grid-cols-2 gap-4">
          <div class="space-y-2">
            <Label for="firstName">{$_("me.settings.first_name")}</Label>
            <Input
              id="firstName"
              placeholder={$_("me.settings.first_name_placeholder")}
              bind:value={firstName}
              required
              class="bg-background/50 border-primary/20 focus:border-primary"
            />
          </div>
          <div class="space-y-2">
            <Label for="lastName">{$_("me.settings.last_name")}</Label>
            <Input
              id="lastName"
              placeholder={$_("me.settings.last_name_placeholder")}
              bind:value={lastName}
              required
              class="bg-background/50 border-primary/20 focus:border-primary"
            />
          </div>
        </div>

        <div class="space-y-2">
          <Label for="artistName">{$_("me.settings.artist_name")}</Label>
          <Input
            id="artistName"
            placeholder={$_("me.settings.artist_name_placeholder")}
            bind:value={artistName}
            required
            class="bg-background/50 border-primary/20 focus:border-primary"
          />
        </div>

        <div class="space-y-2">
          <Label for="description">{$_("me.settings.description")}</Label>
          <Textarea
            id="description"
            bind:value={description}
            placeholder={$_("me.settings.description_placeholder")}
            class="bg-background/50 border-primary/20 focus:border-primary"
            rows={3}
          />
        </div>

        <div class="space-y-2">
          <Label for="tags">{$_("me.settings.tags")}</Label>
          <TagsInput
            id="tags"
            bind:value={tags}
            placeholder={$_("me.settings.tags_placeholder")}
            class="bg-background/50 border-primary/20 focus:border-primary"
          />
        </div>

        {#if isProfileError}
          <div class="grid w-full items-start gap-4">
            <Alert.Root variant="destructive">
              <Alert.Title class="items-center flex">
                <span class="icon-[ri--alert-line] inline-block w-4 h-4 mr-1"></span>
                {$_("me.settings.error")}
              </Alert.Title>
              <Alert.Description>{profileError}</Alert.Description>
            </Alert.Root>
          </div>
        {/if}

        <Button
          type="submit"
          class="cursor-pointer w-full bg-gradient-to-r from-primary to-accent hover:from-primary/90 hover:to-accent/90"
          disabled={isProfileLoading}
        >
          {#if isProfileLoading}
            <div
              class="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin mr-2"
            ></div>
            {$_("me.settings.updating_profile")}
          {:else}
            {$_("me.settings.update_profile")}
          {/if}
        </Button>
      </form>
    </CardContent>
  </Card>
</div>
