import React from "react";
import styled from "styled-components";
import { Link } from 'react-router-dom';
import blogPosts from '../data/blogdata'; // Import the blog data

const BlogCardContainer = styled.div`
  align-self: center;
  width: 100%;  // Makes the card flexible to take full width of its container
  max-width: 320px; // Limits the width for cards (fixed size max)
  margin: 10px; // Consistent margin for even spacing between cards
  border-radius: 12px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  background-color: #2d3748;  // Dark background
  cursor: pointer;
  border: 3px solid #3267B9; // Blue border
  
  // Prevent the card from expanding beyond its max width
  box-sizing: border-box; // Ensure padding doesn't increase width

  // On larger screens, the cards may need to adjust themselves more dynamically
  @media (min-width: 768px) {
    margin: 10px;  // Ensure margin is consistent
  }

  @media (min-width: 1024px) {
    margin: 10px;  // Ensure margin is consistent on larger screens
  }
`;

const BlogImage = styled.img`
  width: 100%;
  height: 180px;
  object-fit: cover;
  border-bottom: 1px solid #e0e0e0;
  // Ensure that the image doesn't stretch out of bounds
  object-position: center;
`;

const BlogContent = styled.div`
  padding: 15px;
  text-align: left;
  flex-grow: 1;
`;

const BlogTitle = styled.h3`
  font-size: 1.2rem;
  color: #e2e8f0;
  font-weight: 600;
  margin-bottom: 10px;
`;

const BlogMeta = styled.div`
  font-size: 0.9rem;
  color: #3f7ad3;
  margin-bottom: 10px;
`;

const BlogExcerpt = styled.p`
  font-size: 1rem;
  color: #e2e8f0;
  margin-bottom: 20px;
  line-height: 1.5;
`;

const ReadMore = styled(Link)`
  font-size: 1rem;
  color: #fff;
  font-weight: bold;
  text-decoration: none;
  display: inline-block;
  padding: 10px 20px;
  margin-top: 10px;
  border-radius: 25px;
  background-color: #3267B9;
  transition: all 0.3s ease-in-out;

  &:hover {
    text-decoration: none;
    box-shadow: 0 0 10px rgba(63, 122, 211, 0.8);
    transform: translateY(-3px);
  }
`;

const BlogCard = ({ blog }) => {
  return (
    <BlogCardContainer data-testid="blog-card">
      <BlogImage src={blog.imageUrl} alt={blog.title} />
      <BlogContent>
        <BlogTitle>{blog.title}</BlogTitle>
        <BlogMeta>
          <span>{blog.author}</span> | <span>{blog.date}</span> | <span>{blog.readTime} min read</span>
        </BlogMeta>
        <BlogExcerpt>{blog.excerpt}</BlogExcerpt>
        
        {/* Pass post as state to the route */}
        <center>
          <ReadMore to={`/blog/${blog.id}`} state={{ post: blogPosts.find(post=>post.id===blog.id) }}>
            Read more →
          </ReadMore>
        </center>
      </BlogContent>
    </BlogCardContainer>
  );
};

export default BlogCard;