<template>
  <div class="space-y-6">
    <UFormField label="What's on your mind?" name="content" required>
      <UTextarea
        v-model="formData.content"
        placeholder="Share your thoughts, updates, questions, or learnings with the community..."
        :rows="8"
        autoresize
        :maxrows="20"
      />
    </UFormField>

    <!-- Privacy Settings -->
    <div class="border border-guild-700 rounded-lg p-4 bg-guild-800/30">
      <h3 class="text-sm font-medium text-guild-200 mb-4">Privacy Settings</h3>
      <div class="space-y-3">
        <label class="flex items-center gap-3 cursor-pointer">
          <input
            v-model="formData.privacy"
            type="radio"
            value="public"
            class="w-4 h-4 text-guild-400"
          />
          <div>
            <div class="text-guild-200 font-medium">Public</div>
            <div class="text-sm text-guild-400">
              Visible to everyone, including non-members
            </div>
          </div>
        </label>

        <label class="flex items-center gap-3 cursor-pointer">
          <input
            v-model="formData.privacy"
            type="radio"
            value="members"
            class="w-4 h-4 text-guild-400"
          />
          <div>
            <div class="text-guild-200 font-medium">Members Only</div>
            <div class="text-sm text-guild-400">
              Only visible to Ghost Guild members
            </div>
          </div>
        </label>

        <label class="flex items-center gap-3 cursor-pointer">
          <input
            v-model="formData.privacy"
            type="radio"
            value="private"
            class="w-4 h-4 text-guild-400"
          />
          <div>
            <div class="text-guild-200 font-medium">Private</div>
            <div class="text-sm text-guild-400">Only visible to you</div>
          </div>
        </label>
      </div>
    </div>

    <!-- Image Upload (Future) -->
    <!-- TODO: Add image upload integration with Cloudinary -->

    <!-- Comments Toggle -->
    <div class="flex items-center gap-3">
      <USwitch v-model="formData.commentsEnabled" />
      <div>
        <div class="text-guild-200 font-medium">Enable Comments</div>
        <div class="text-sm text-guild-400">
          Allow members to comment on this update
        </div>
      </div>
    </div>

    <!-- Actions -->
    <div
      class="flex justify-between items-center pt-4 border-t border-guild-700"
    >
      <UButton variant="ghost" color="neutral" @click="$emit('cancel')">
        Cancel
      </UButton>

      <UButton
        :loading="submitting"
        :disabled="!formData.content.trim()"
        @click="handleSubmit"
      >
        {{ submitLabel }}
      </UButton>
    </div>

    <!-- Error Message -->
    <div
      v-if="error"
      class="bg-ember-900/20 border border-ember-400/30 rounded-lg p-4"
    >
      <p class="text-ember-400">{{ error }}</p>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  initialData: {
    type: Object,
    default: () => ({
      content: "",
      privacy: "members",
      commentsEnabled: true,
      images: [],
    }),
  },
  submitLabel: {
    type: String,
    default: "Post Update",
  },
  submitting: {
    type: Boolean,
    default: false,
  },
  error: {
    type: String,
    default: null,
  },
});

const emit = defineEmits(["submit", "cancel"]);

const formData = reactive({
  content: props.initialData.content || "",
  privacy: props.initialData.privacy || "members",
  commentsEnabled: props.initialData.commentsEnabled ?? true,
  images: props.initialData.images || [],
});

const handleSubmit = () => {
  if (!formData.content.trim()) return;
  emit("submit", { ...formData });
};

// Watch for initialData changes (for edit mode)
watch(
  () => props.initialData,
  (newData) => {
    if (newData) {
      formData.content = newData.content || "";
      formData.privacy = newData.privacy || "members";
      formData.commentsEnabled = newData.commentsEnabled ?? true;
      formData.images = newData.images || [];
    }
  },
  { immediate: true },
);
</script>

<style scoped>
/* Field labels */
:deep(label) {
  color: var(--color-guild-200) !important;
  font-weight: 500;
}

/* Textarea styling */
:deep(textarea) {
  background-color: var(--color-guild-800) !important;
  color: var(--color-guild-200) !important;
  border-color: var(--color-guild-600) !important;
}

:deep(textarea::placeholder) {
  color: var(--color-guild-500) !important;
}

:deep(textarea:focus) {
  border-color: var(--color-guild-400) !important;
  background-color: var(--color-guild-700) !important;
}

/* Radio buttons */
input[type="radio"] {
  accent-color: var(--color-candlelight-600);
}
</style>
