import React, { useState, useRef, useEffect } from 'react';
import styled from 'styled-components';

interface AccordionProps {
    title: string;
    children: React.ReactNode;
}

const AccordionContainer = styled.div`
    padding: 30px 62px;
    background: #15241E;
    border-radius: 20px;
    width: 878px;
    margin-bottom: 25px;
    cursor: pointer;
    @media (max-width: 970px) {
        width: 90%;
        padding: 25px 40px;
    }
    @media (max-width: 750px) {
        padding: 20px 25px;
        border-radius: 15px;
        margin-bottom: 15px;
    }
    @media (max-width: 480px) {
        padding: 15px 20px;
        width: 95%;
        margin-bottom: 12px;
    }
`;

const AccordionTitle = styled.div`
    font-size: 40px;
    text-transform: uppercase;
    display: flex;
    justify-content: space-between;
    line-height: 1;
    width: 100%;
    gap: 14px;
    align-items: center;
    text-align: start;
    h3 {
        max-width: 580px;
        margin: 0;
    }
    @media (max-width: 970px) {
        font-size: 32px;
        gap: 12px;
    }
    @media (max-width: 750px) {
        font-size: 28px;
        gap: 10px;
    }
    @media (max-width: 480px) {
        font-size: 22px;
        gap: 8px;
        flex-direction: row;
        justify-content: space-between;
        h3 {
            flex: 1;
        }
    }
`;

const AccordionContentWrapper = styled.div<{ $maxHeight: number }>`
    overflow: hidden;
    transition: max-height 0.4s ease;
    max-height: ${({ $maxHeight }) => $maxHeight}px;
`;

const AccordionContent = styled.div.attrs({})<{ $isOpen: boolean }>`
    opacity: ${({ $isOpen }) => ($isOpen ? 1 : 0)};
    transform: ${({ $isOpen }) => ($isOpen ? 'translateY(0)' : 'translateY(-10px)')};
    transition: opacity 0.3s ease, transform 0.3s ease;

    p {
        font-size: 20px;
        text-align: start;
        width: 100%;
        margin-top: 14px;
        text-transform: none;
        max-width: 520px;
    }
    @media (max-width: 750px) {
        p {
            font-size: 18px;
            margin-top: 12px;
        }
    }
    @media (max-width: 480px) {
        p {
            font-size: 16px;
            margin-top: 10px;
            line-height: 1.4;
        }
    }
`;

const PlusIcon = styled.span<{ $isOpen: boolean }>`
    transition: transform 0.3s ease;
    font-size: 40px;
    transform: ${({ $isOpen }) => ($isOpen ? 'rotate(45deg)' : 'rotate(0)')};
    display: flex;
    align-items: center;
    justify-content: center;
    
    @media (max-width: 750px) {
        font-size: 32px;
    }
    @media (max-width: 480px) {
        font-size: 24px;
    }
`;

const Accordion = ({ title, children }: AccordionProps) => {
    const [isOpen, setIsOpen] = useState(false);
    const [height, setHeight] = useState(0);
    const contentRef = useRef<HTMLDivElement>(null);

    useEffect(() => {
        if (contentRef.current) {
            setHeight(isOpen ? contentRef.current.scrollHeight+20 : 0);
        }
    }, [isOpen]);

    return (
        <AccordionContainer onClick={() => setIsOpen(!isOpen)}>
            <AccordionTitle>
                <h3>{title}</h3>
                <PlusIcon $isOpen={isOpen}>+</PlusIcon>
            </AccordionTitle>
            <AccordionContentWrapper $maxHeight={height}>
                <AccordionContent $isOpen={isOpen} ref={contentRef}>
                    <p>{children}</p>
                </AccordionContent>
            </AccordionContentWrapper>
        </AccordionContainer>
    );
};

export default Accordion;
