<script module lang="ts">
  import Countdown from "./Countdown.svelte";
  import { componentDecorator } from "../../stories/component-decorator";
  import { localizations } from "../../stories/fixtures";
  import { localizationDecorator } from "../../stories/localization-decorator";
  import { variablesDecorator } from "../../stories/variables-decorator";
  import type { CountdownProps } from "../../types/components/countdown";
  import type { StackProps } from "../../types/components/stack";
  import { DEFAULT_TEXT_COLOR } from "../../utils/constants";
  import { defineMeta } from "@storybook/addon-svelte-csf";
  import MockDate from "mockdate";

  const defaultLocale = Object.keys(localizations)[0];

  // Freeze date for consistent Chromatic snapshots
  // Using a fixed date: 2024-01-15T12:00:00Z
  const frozenDate = new Date("2024-01-15T12:00:00Z");

  // Create dates relative to frozen date for consistent stories
  // Target: 1 day, 23 hours, 45 minutes, 56 seconds from frozen date
  // This equals 47 hours, 45 minutes, 56 seconds = 2024-01-17T11:45:56Z
  const futureDate = new Date(frozenDate);
  futureDate.setDate(futureDate.getDate() + 1); // +1 day = 24 hours
  futureDate.setHours(futureDate.getHours() + 23); // +23 hours (total 47 hours from start)
  futureDate.setMinutes(futureDate.getMinutes() + 45); // +45 minutes
  futureDate.setSeconds(futureDate.getSeconds() + 56); // +56 seconds

  // 3 days from frozen date (for multi-day story)
  const threeDaysFromNow = new Date(frozenDate);
  threeDaysFromNow.setDate(threeDaysFromNow.getDate() + 3);
  threeDaysFromNow.setHours(threeDaysFromNow.getHours() + 5); // Add 5 hours to show hours too

  // Create a date in the past for ended countdown
  const pastDate = new Date(frozenDate);
  pastDate.setHours(pastDate.getHours() - 1);

  const countdownStackProps = () =>
    ({
      type: "stack",
      id: "countdown-stack",
      name: "Countdown Stack",
      components: [
        {
          type: "text",
          id: "countdown-text",
          name: "Countdown Text",
          text_lid: "countdown",
          color: {
            light: { type: "hex", value: DEFAULT_TEXT_COLOR },
          },
          font_size: "heading_xl",
          font_weight: "bold",
          horizontal_alignment: "center",
          size: {
            width: { type: "fit" },
            height: { type: "fit" },
          },
          margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          background_color: null,
        },
      ],
      size: {
        width: { type: "fit" },
        height: { type: "fit" },
      },
      dimension: {
        type: "vertical",
        alignment: "center",
        distribution: "center",
      },
      spacing: 8,
      margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
      padding: { top: 16, trailing: 16, bottom: 16, leading: 16 },
      background_color: {
        light: { type: "hex", value: "#F0F0F0" },
      },
      background: null,
      border: null,
      shape: {
        type: "rectangle",
        corners: {
          top_leading: 8,
          top_trailing: 8,
          bottom_leading: 8,
          bottom_trailing: 8,
        },
      },
      shadow: null,
      badge: null,
    }) satisfies StackProps;

  const countdownProps = () =>
    ({
      type: "countdown",
      id: "countdown",
      name: "Countdown",
      style: {
        type: "date",
        date: futureDate.toISOString(),
      },
      countdown_stack: countdownStackProps(),
      end_stack: {
        type: "stack",
        id: "end-stack",
        name: "End Stack",
        components: [
          {
            type: "text",
            id: "end-text",
            name: "End Text",
            text_lid: "ended",
            color: {
              light: { type: "hex", value: "#FF0000" },
            },
            font_size: "heading_xl",
            font_weight: "bold",
            horizontal_alignment: "center",
            size: {
              width: { type: "fit" },
              height: { type: "fit" },
            },
            margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
            padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
            background_color: null,
          },
        ],
        size: {
          width: { type: "fit" },
          height: { type: "fit" },
        },
        dimension: {
          type: "vertical",
          alignment: "center",
          distribution: "center",
        },
        spacing: 8,
        margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
        padding: { top: 16, trailing: 16, bottom: 16, leading: 16 },
        background_color: {
          light: { type: "hex", value: "#FFE0E0" },
        },
        background: null,
        border: null,
        shape: {
          type: "rectangle",
          corners: {
            top_leading: 8,
            top_trailing: 8,
            bottom_leading: 8,
            bottom_trailing: 8,
          },
        },
        shadow: null,
        badge: null,
      },
    }) satisfies CountdownProps;

  const { Story } = defineMeta({
    title: "Components/Countdown",
    component: Countdown,
    decorators: [
      componentDecorator(),
      localizationDecorator({
        defaultLocale,
        localizations,
      }),
      variablesDecorator(undefined),
    ],
    beforeEach: () => {
      MockDate.set(frozenDate);
      return () => MockDate.reset();
    },
    args: countdownProps(),
  });
</script>

<Story
  name="Active Countdown"
  args={{
    style: {
      type: "date",
      date: futureDate.toISOString(),
    },
  }}
/>

<Story
  name="Ended Countdown"
  args={{
    style: {
      type: "date",
      date: pastDate.toISOString(),
    },
  }}
/>

<Story
  name="Countdown with Variables"
  args={{
    style: {
      type: "date",
      date: futureDate.toISOString(),
    },
    countdown_stack: {
      type: "stack",
      id: "countdown-stack-vars",
      name: "Countdown Stack with Variables",
      components: [
        {
          type: "text",
          id: "days",
          name: "Days",
          text_lid: "days",
          color: {
            light: { type: "hex", value: DEFAULT_TEXT_COLOR },
          },
          font_size: "heading_xxl",
          font_weight: "bold",
          horizontal_alignment: "center",
          size: {
            width: { type: "fit" },
            height: { type: "fit" },
          },
          margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          background_color: null,
        },
        {
          type: "text",
          id: "time",
          name: "Time",
          text_lid: "time",
          color: {
            light: { type: "hex", value: "#666666" },
          },
          font_size: "heading_l",
          font_weight: "regular",
          horizontal_alignment: "center",
          size: {
            width: { type: "fit" },
            height: { type: "fit" },
          },
          margin: { top: 8, trailing: 0, bottom: 0, leading: 0 },
          padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          background_color: null,
        },
      ],
      size: {
        width: { type: "fit" },
        height: { type: "fit" },
      },
      dimension: {
        type: "vertical",
        alignment: "center",
        distribution: "center",
      },
      spacing: 0,
      margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
      padding: { top: 32, trailing: 32, bottom: 32, leading: 32 },
      background_color: {
        light: { type: "hex", value: "#E8F5E9" },
      },
      background: null,
      border: {
        width: 2,
        color: {
          light: { type: "hex", value: "#4CAF50" },
        },
      },
      shape: {
        type: "rectangle",
        corners: {
          top_leading: 16,
          top_trailing: 16,
          bottom_leading: 16,
          bottom_trailing: 16,
        },
      },
      shadow: null,
      badge: null,
    },
  }}
/>

<Story
  name="Countdown with Multiple Days"
  args={{
    style: {
      type: "date",
      date: threeDaysFromNow.toISOString(),
    },
    countdown_stack: {
      type: "stack",
      id: "countdown-stack-multi-day",
      name: "Countdown Stack with Multiple Days",
      components: [
        {
          type: "text",
          id: "days",
          name: "Days",
          text_lid: "days",
          color: {
            light: { type: "hex", value: DEFAULT_TEXT_COLOR },
          },
          font_size: "heading_xxl",
          font_weight: "bold",
          horizontal_alignment: "center",
          size: {
            width: { type: "fit" },
            height: { type: "fit" },
          },
          margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          background_color: null,
        },
        {
          type: "text",
          id: "time",
          name: "Time",
          text_lid: "time",
          color: {
            light: { type: "hex", value: "#666666" },
          },
          font_size: "heading_l",
          font_weight: "regular",
          horizontal_alignment: "center",
          size: {
            width: { type: "fit" },
            height: { type: "fit" },
          },
          margin: { top: 8, trailing: 0, bottom: 0, leading: 0 },
          padding: { top: 0, trailing: 0, bottom: 0, leading: 0 },
          background_color: null,
        },
      ],
      size: {
        width: { type: "fit" },
        height: { type: "fit" },
      },
      dimension: {
        type: "vertical",
        alignment: "center",
        distribution: "center",
      },
      spacing: 0,
      margin: { top: 0, trailing: 0, bottom: 0, leading: 0 },
      padding: { top: 32, trailing: 32, bottom: 32, leading: 32 },
      background_color: {
        light: { type: "hex", value: "#E3F2FD" },
      },
      background: null,
      border: {
        width: 2,
        color: {
          light: { type: "hex", value: "#2196F3" },
        },
      },
      shape: {
        type: "rectangle",
        corners: {
          top_leading: 16,
          top_trailing: 16,
          bottom_leading: 16,
          bottom_trailing: 16,
        },
      },
      shadow: null,
      badge: null,
    },
  }}
/>

<Story
  name="Border"
  args={{
    ...countdownProps(),
    countdown_stack: {
      ...countdownStackProps(),
      border: {
        width: 10,
        color: {
          light: {
            type: "linear",
            degrees: 90,
            points: [
              { color: "#000000", percent: 0 },
              { color: "#FF0000", percent: 100 },
            ],
          },
        },
      },
    },
  }}
/>
