// Social feed screens

const PostCardFull = ({ post, onLike, onOpen, onOpenUser }) => (
  <div style={{ marginBottom: 24 }}>
    <div
      style={{
        display: "flex",
        alignItems: "center",
        padding: "0 20px 12px",
        gap: 10,
      }}
    >
      <button
        onClick={() => onOpenUser?.(post.user.handle)}
        style={{
          padding: 0,
          background: "none",
          border: "none",
          cursor: "pointer",
        }}
      >
        <Avatar user={post.user} size={36} ring />
      </button>
      <button
        onClick={() => onOpenUser?.(post.user.handle)}
        style={{
          flex: 1,
          minWidth: 0,
          padding: 0,
          background: "none",
          border: "none",
          cursor: "pointer",
          textAlign: "left",
          font: "inherit",
          color: "inherit",
        }}
      >
        <div
          style={{
            fontSize: 13,
            fontWeight: 600,
            display: "flex",
            alignItems: "center",
            gap: 5,
          }}
        >
          {post.user.name}
          {post.verified && (
            <span
              style={{
                width: 14,
                height: 14,
                borderRadius: "50%",
                background: "var(--amber)",
                display: "inline-flex",
                alignItems: "center",
                justifyContent: "center",
                color: "#05080F",
                flexShrink: 0,
              }}
            >
              <IconCheck size={9} stroke={3} />
            </span>
          )}
        </div>
        <div style={{ fontSize: 11, color: "var(--text-mute)" }}>
          @{post.user.handle} · {post.time}
        </div>
      </button>
      <button
        style={{
          background: "none",
          border: "none",
          color: "var(--text-mute)",
          cursor: "pointer",
          padding: 4,
        }}
      >
        <IconMore size={18} />
      </button>
    </div>

    <button
      onClick={() => onOpen(post)}
      style={{
        width: "100%",
        background: "none",
        border: "none",
        padding: 0,
        cursor: "pointer",
        display: "block",
      }}
    >
      <Placeholder gradient={post.image} aspect="1/1" label={post.imageLabel} />
    </button>

    <div
      style={{
        padding: "12px 20px 0",
        display: "flex",
        gap: 16,
        alignItems: "center",
      }}
    >
      <button
        onClick={() => onLike(post.id)}
        style={{
          background: "none",
          border: "none",
          color: post.liked ? "var(--amber)" : "#fff",
          cursor: "pointer",
          display: "flex",
          alignItems: "center",
          gap: 6,
          padding: 0,
          font: "inherit",
        }}
      >
        {post.liked ? <IconHeartFilled size={22} /> : <IconHeart size={22} />}
        <span style={{ fontSize: 13, fontWeight: 500 }}>{post.likes}</span>
      </button>
      <button
        onClick={() => onOpen(post)}
        style={{
          background: "none",
          border: "none",
          color: "#fff",
          cursor: "pointer",
          display: "flex",
          alignItems: "center",
          gap: 6,
          padding: 0,
          font: "inherit",
        }}
      >
        <IconMessage size={22} />
        <span style={{ fontSize: 13, fontWeight: 500 }}>{post.comments}</span>
      </button>
      <button
        style={{
          background: "none",
          border: "none",
          color: "#fff",
          cursor: "pointer",
          padding: 0,
        }}
      >
        <IconShare size={20} />
      </button>
    </div>

    <div style={{ padding: "10px 20px 0", fontSize: 14, lineHeight: 1.4 }}>
      <span style={{ fontWeight: 600 }}>{post.user.handle}</span>
      <span style={{ color: "var(--text-dim)" }}> {post.caption}</span>
    </div>

    {post.comments > 0 && (
      <button
        onClick={() => onOpen(post)}
        style={{
          background: "none",
          border: "none",
          color: "var(--text-mute)",
          fontSize: 12,
          padding: "6px 20px 0",
          cursor: "pointer",
          font: "inherit",
        }}
      >
        Ver todos os {post.comments} comentários
      </button>
    )}
  </div>
);

const FeedList = ({ posts, onLike, onOpen, onOpenUser }) => (
  <div style={{ paddingTop: 4 }}>
    {posts.map((p) => (
      <PostCardFull
        key={p.id}
        post={p}
        onLike={onLike}
        onOpen={onOpen}
        onOpenUser={onOpenUser}
      />
    ))}
  </div>
);

const FeedGrid = ({ posts, onOpen }) => (
  <div
    style={{
      padding: "4px 2px",
      display: "grid",
      gridTemplateColumns: "repeat(3,1fr)",
      gap: 2,
    }}
  >
    {posts
      .concat(posts)
      .concat(posts)
      .slice(0, 12)
      .map((p, i) => (
        <button
          key={i}
          onClick={() => onOpen(p)}
          style={{
            padding: 0,
            border: "none",
            background: "none",
            cursor: "pointer",
          }}
        >
          <Placeholder gradient={p.image} aspect="1/1" />
        </button>
      ))}
  </div>
);

const FeedCompact = ({ posts, onLike, onOpen }) => (
  <div style={{ padding: "0 12px" }}>
    {posts.map((p) => (
      <div
        key={p.id}
        style={{
          display: "flex",
          gap: 12,
          padding: 10,
          marginBottom: 8,
          background: "rgba(255,255,255,0.03)",
          border: "1px solid var(--line)",
          borderRadius: 16,
        }}
      >
        <button
          onClick={() => onOpen(p)}
          style={{
            padding: 0,
            border: "none",
            background: "none",
            cursor: "pointer",
            flexShrink: 0,
          }}
        >
          <div
            style={{
              width: 80,
              height: 80,
              borderRadius: 12,
              overflow: "hidden",
            }}
          >
            <Placeholder gradient={p.image} height={80} />
          </div>
        </button>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div
            style={{
              display: "flex",
              alignItems: "center",
              gap: 6,
              marginBottom: 4,
            }}
          >
            <Avatar user={p.user} size={20} />
            <span style={{ fontSize: 12, fontWeight: 600 }}>
              {p.user.handle}
            </span>
            <span
              style={{
                fontSize: 10,
                color: "var(--text-mute)",
                marginLeft: "auto",
              }}
            >
              {p.time}
            </span>
          </div>
          <div
            style={{
              fontSize: 12,
              color: "var(--text-dim)",
              lineHeight: 1.4,
              display: "-webkit-box",
              WebkitLineClamp: 2,
              WebkitBoxOrient: "vertical",
              overflow: "hidden",
            }}
          >
            {p.caption}
          </div>
          <div
            style={{
              display: "flex",
              gap: 10,
              marginTop: 8,
              fontSize: 11,
              color: "var(--text-mute)",
            }}
          >
            <span
              style={{ display: "flex", alignItems: "center", gap: 3 }}
              onClick={() => onLike(p.id)}
            >
              {p.liked ? (
                <IconHeartFilled size={12} />
              ) : (
                <IconHeart size={12} />
              )}{" "}
              {p.likes}
            </span>
            <span style={{ display: "flex", alignItems: "center", gap: 3 }}>
              <IconMessage size={12} /> {p.comments}
            </span>
          </div>
        </div>
      </div>
    ))}
  </div>
);

const FeedScreen = ({
  layout,
  posts,
  onLike,
  onOpen,
  onOpenUser,
  onCreate,
  onNotifications,
}) => {
  return (
    <div className="screen">
      <StatusBar />

      {/* Title bar */}
      <div
        style={{
          padding: "4px 20px 16px",
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
        }}
      >
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div
            style={{
              display: "flex",
              alignItems: "center",
              gap: 12,
              width: 44,
              height: 44,
            }}
          >
            <img
              src="src/logo.png"
              alt="IBMEC"
              style={{ borderRadius: "50%", width: 44, height: 44 }}
            />
          </div>
          <div>
            <div
              style={{
                fontSize: 18,
                fontWeight: 600,
                letterSpacing: "-0.01em",
              }}
            >
              Mural
            </div>
            <div
              className="mono"
              style={{
                fontSize: 9,
                color: "var(--text-mute)",
                letterSpacing: "0.14em",
              }}
            >
              FORMA TOUR
            </div>
          </div>
        </div>
        <div style={{ display: "flex", gap: 8 }}>
          <button
            className="btn btn-nav"
            style={{ position: "relative" }}
            onClick={onNotifications}
          >
            <IconBell size={18} />
            <span
              style={{
                position: "absolute",
                top: 6,
                right: 6,
                width: 8,
                height: 8,
                borderRadius: "50%",
                background: "var(--amber)",
                border: "1.5px solid #05080F",
              }}
            />
          </button>
        </div>
      </div>

      <div style={{ height: 1, background: "var(--line)", margin: "0 20px" }} />

      <div style={{ paddingTop: 20 }}>
        {layout === "list" && (
          <FeedList
            posts={posts}
            onLike={onLike}
            onOpen={onOpen}
            onOpenUser={onOpenUser}
          />
        )}
        {layout === "grid" && <FeedGrid posts={posts} onOpen={onOpen} />}
        {layout === "compact" && (
          <FeedCompact posts={posts} onLike={onLike} onOpen={onOpen} />
        )}
      </div>
    </div>
  );
};

const PostDetailScreen = ({ post, onBack, onLike, onOpenUser }) => {
  const [newComment, setNewComment] = React.useState("");
  const [comments, setComments] = React.useState(COMMENTS);

  const submit = () => {
    if (!newComment.trim()) return;
    setComments([
      ...comments,
      {
        id: "c" + Date.now(),
        user: CURRENT_USER.name,
        avatar: CURRENT_USER.avatar,
        color: CURRENT_USER.color,
        time: "agora",
        text: newComment,
        likes: 0,
      },
    ]);
    setNewComment("");
  };

  return (
    <div
      className="screen"
      style={{ display: "flex", flexDirection: "column" }}
    >
      <StatusBar />
      <ScreenHeader
        title="Publicação"
        onBack={onBack}
        right={
          <button
            className="btn btn-nav"
            style={{ border: "none", background: "none" }}
          >
            <IconMore size={18} />
          </button>
        }
      />

      <div style={{ flex: 1, overflowY: "auto", paddingBottom: 120 }}>
        <button
          onClick={() => onOpenUser?.(post.user.handle)}
          style={{
            padding: "0 20px 14px",
            display: "flex",
            alignItems: "center",
            gap: 10,
            width: "100%",
            background: "none",
            border: "none",
            cursor: "pointer",
            font: "inherit",
            color: "inherit",
            textAlign: "left",
          }}
        >
          <Avatar user={post.user} size={36} />
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 13, fontWeight: 600 }}>
              {post.user.name}
            </div>
            <div style={{ fontSize: 11, color: "var(--text-mute)" }}>
              @{post.user.handle} · {post.time}
            </div>
          </div>
        </button>
        <Placeholder
          gradient={post.image}
          aspect="1/1"
          label={post.imageLabel}
        />
        <div style={{ padding: "12px 20px", display: "flex", gap: 16 }}>
          <button
            onClick={() => onLike(post.id)}
            style={{
              background: "none",
              border: "none",
              color: post.liked ? "var(--amber)" : "#fff",
              display: "flex",
              alignItems: "center",
              gap: 6,
              cursor: "pointer",
              font: "inherit",
              padding: 0,
            }}
          >
            {post.liked ? (
              <IconHeartFilled size={22} />
            ) : (
              <IconHeart size={22} />
            )}
            <span style={{ fontSize: 13 }}>{post.likes}</span>
          </button>
          <span style={{ display: "flex", alignItems: "center", gap: 6 }}>
            <IconMessage size={22} />{" "}
            <span style={{ fontSize: 13 }}>{comments.length}</span>
          </span>
          <IconShare size={22} style={{ marginLeft: "auto" }} />
        </div>
        <div
          style={{
            padding: "0 20px",
            fontSize: 14,
            lineHeight: 1.5,
            color: "var(--text-dim)",
            marginBottom: 16,
          }}
        >
          <span style={{ color: "#fff", fontWeight: 600 }}>
            {post.user.handle}
          </span>{" "}
          {post.caption}
        </div>

        <div
          style={{ borderTop: "1px solid var(--line)", padding: "16px 20px 0" }}
        >
          <div
            className="mono"
            style={{
              fontSize: 10,
              letterSpacing: "0.14em",
              color: "var(--text-mute)",
              marginBottom: 14,
            }}
          >
            {comments.length} COMENTÁRIOS
          </div>
          {comments.map((c) => (
            <div
              key={c.id}
              style={{ display: "flex", gap: 10, marginBottom: 16 }}
            >
              <Avatar user={{ avatar: c.avatar, color: c.color }} size={32} />
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 13, lineHeight: 1.4 }}>
                  <span style={{ fontWeight: 600 }}>{c.user}</span>
                  <span style={{ color: "var(--text-dim)" }}> {c.text}</span>
                </div>
                <div
                  style={{
                    display: "flex",
                    gap: 12,
                    marginTop: 4,
                    fontSize: 11,
                    color: "var(--text-mute)",
                  }}
                >
                  <span>{c.time}</span>
                  <span>{c.likes} curtidas</span>
                  <span>Responder</span>
                </div>
              </div>
              <button
                style={{
                  background: "none",
                  border: "none",
                  color: "var(--text-mute)",
                  cursor: "pointer",
                }}
              >
                <IconHeart size={14} />
              </button>
            </div>
          ))}
        </div>
      </div>

      <div
        style={{
          position: "absolute",
          left: 0,
          right: 0,
          bottom: 0,
          padding: "12px 16px 20px",
          background: "rgba(5,8,15,0.95)",
          backdropFilter: "blur(16px)",
          borderTop: "1px solid var(--line)",
          display: "flex",
          gap: 10,
          alignItems: "center",
        }}
      >
        <Avatar user={CURRENT_USER} size={32} />
        <input
          className="input"
          value={newComment}
          onChange={(e) => setNewComment(e.target.value)}
          onKeyDown={(e) => e.key === "Enter" && submit()}
          placeholder={`Comentar como @${CURRENT_USER.handle}…`}
          style={{ padding: "10px 14px", fontSize: 13 }}
        />
        <button
          onClick={submit}
          disabled={!newComment.trim()}
          style={{
            background: newComment.trim()
              ? "var(--amber)"
              : "rgba(255,255,255,0.06)",
            color: newComment.trim() ? "#05080F" : "var(--text-mute)",
            border: "none",
            borderRadius: 12,
            width: 40,
            height: 40,
            cursor: "pointer",
            flexShrink: 0,
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          <IconArrowRight size={18} />
        </button>
      </div>
    </div>
  );
};

const CreatePostScreen = ({ onBack, onPost }) => {
  const [caption, setCaption] = React.useState("");
  const [selected, setSelected] = React.useState(null); // null until user picks / takes
  const fileRef = React.useRef(null);

  const pick = () => {
    // Simulate picking from library — cycle through placeholder gradients
    const gradients = [
      "ph-gradient-1",
      "ph-gradient-2",
      "ph-gradient-3",
      "ph-gradient-4",
      "ph-gradient-5",
      "ph-gradient-6",
    ];
    const idx = selected
      ? (gradients.indexOf(selected) + 1) % gradients.length
      : 0;
    setSelected(gradients[idx]);
  };
  const capture = () => {
    const gradients = ["ph-gradient-3", "ph-gradient-4", "ph-gradient-6"];
    setSelected(gradients[Math.floor(Math.random() * gradients.length)]);
  };

  return (
    <div className="screen">
      <StatusBar />
      <div
        style={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          padding: "8px 20px 16px",
        }}
      >
        <button className="btn btn-nav" onClick={onBack}>
          <IconClose size={18} />
        </button>
        <div style={{ fontSize: 16, fontWeight: 600 }}>Nova publicação</div>
        <button
          onClick={() =>
            onPost({ caption, image: selected || "ph-gradient-3" })
          }
          disabled={!caption.trim() || !selected}
          style={{
            background:
              caption.trim() && selected
                ? "var(--amber)"
                : "rgba(255,255,255,0.06)",
            color: caption.trim() && selected ? "#05080F" : "var(--text-mute)",
            border: "none",
            borderRadius: 12,
            padding: "8px 14px",
            cursor: "pointer",
            font: "inherit",
            fontSize: 13,
            fontWeight: 600,
          }}
        >
          Publicar
        </button>
      </div>

      <div style={{ padding: "0 20px 20px" }}>
        {selected ? (
          <div style={{ position: "relative", marginBottom: 16 }}>
            <Placeholder gradient={selected} aspect="4/5" label="" />
            <button
              onClick={() => setSelected(null)}
              style={{
                position: "absolute",
                top: 12,
                right: 12,
                background: "rgba(5,8,15,0.7)",
                backdropFilter: "blur(10px)",
                border: "1px solid var(--line-strong)",
                color: "#fff",
                borderRadius: 999,
                width: 32,
                height: 32,
                cursor: "pointer",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
              }}
            >
              <IconClose size={14} />
            </button>
          </div>
        ) : (
          <div
            style={{
              aspectRatio: "4/5",
              borderRadius: 16,
              border: "1.5px dashed var(--line-strong)",
              background: "rgba(255,255,255,0.02)",
              display: "flex",
              flexDirection: "column",
              alignItems: "center",
              justifyContent: "center",
              gap: 14,
              marginBottom: 16,
              padding: 20,
            }}
          >
            <div
              style={{
                width: 56,
                height: 56,
                borderRadius: 16,
                background: "rgba(245,166,35,0.12)",
                color: "var(--amber)",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
              }}
            >
              <IconCamera size={24} />
            </div>
            <div style={{ textAlign: "center" }}>
              <div style={{ fontSize: 15, fontWeight: 600, marginBottom: 4 }}>
                Adicione uma foto
              </div>
              <div style={{ fontSize: 12, color: "var(--text-mute)" }}>
                Tire uma nova ou escolha da galeria
              </div>
            </div>
            <div
              style={{ display: "flex", gap: 8, width: "100%", maxWidth: 260 }}
            >
              <button
                onClick={capture}
                className="btn btn-primary"
                style={{ flex: 1, fontSize: 13 }}
              >
                <IconCamera size={14} /> Câmera
              </button>
              <button
                onClick={pick}
                className="btn btn-ghost"
                style={{ flex: 1, fontSize: 13 }}
              >
                <IconGrid size={14} /> Galeria
              </button>
            </div>
            <input
              ref={fileRef}
              type="file"
              accept="image/*"
              style={{ display: "none" }}
            />
          </div>
        )}

        <textarea
          className="input"
          value={caption}
          onChange={(e) => setCaption(e.target.value)}
          placeholder="Escreva uma legenda..."
          style={{ minHeight: 110, resize: "none", fontFamily: "inherit" }}
        />
      </div>
    </div>
  );
};

Object.assign(window, { FeedScreen, PostDetailScreen, CreatePostScreen });
