import type { CSSProperties } from "react";

type ProjectBackground = {
  backgroundColor?: string;
  backgroundPicture?: string;
};

const hexToRgba = (hex: string, alpha: number) => {
  const normalized = hex.replace("#", "");
  const r = parseInt(normalized.slice(0, 2), 16);
  const g = parseInt(normalized.slice(2, 4), 16);
  const b = parseInt(normalized.slice(4, 6), 16);
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};

export const getProjectBackgroundStyle = (project: ProjectBackground): CSSProperties => {
  if (project.backgroundPicture) {
    return {
      backgroundImage: `url(${project.backgroundPicture})`,
      backgroundSize: "cover",
      backgroundPosition: "center",
    };
  }

  const color = project.backgroundColor ?? "#A8E6CF";

  return {
    background: `linear-gradient(135deg, ${color}, ${hexToRgba(color, 0.5)})`,
  };
};
