document.addEventListener('DOMContentLoaded', function() {
    const canvas = document.getElementById('ratingChart');
    if (!canvas) return;

    const ctx = canvas.getContext('2d');
    const width = canvas.width;
    const height = canvas.height;
    const centerX = width / 2;
    const centerY = (height / 2) + 15; // Adjust center point to be between labels
    const radius = Math.min(width, height) * 0.28; // Make pentagon slightly smaller

    // Rating data - ordered to match the visual layout
    const ratings = [
        { name: 'Regulation', value: 9.35 },
        { name: 'Business', value: 9.25 },
        { name: 'Risk Control', value: 9.93 },
        { name: 'Software', value: 9.99 },
        { name: 'License', value: 9.09 }
    ];

    function drawPentagon(ctx, centerX, centerY, radius, rotation = -Math.PI/2) {
        const points = [];
        for (let i = 0; i < 5; i++) {
            const angle = rotation + (i * 2 * Math.PI) / 5;
            points.push({
                x: centerX + radius * Math.cos(angle),
                y: centerY + radius * Math.sin(angle)
            });
        }
        return points;
    }

    function drawChart() {
        // Clear canvas
        ctx.clearRect(0, 0, width, height);

        // Draw background pentagon
        const backgroundPoints = drawPentagon(ctx, centerX, centerY, radius);
        ctx.beginPath();
        backgroundPoints.forEach((point, index) => {
            if (index === 0) ctx.moveTo(point.x, point.y);
            else ctx.lineTo(point.x, point.y);
        });
        ctx.closePath();
        ctx.strokeStyle = '#EEEEEE';
        ctx.lineWidth = 1;
        ctx.stroke();
        ctx.fillStyle = '#FFFFFF';
        ctx.fill();

        // Draw data pentagon
        const dataPoints = ratings.map((rating, index) => {
            const angle = -Math.PI/2 + (index * 2 * Math.PI) / 5;
            const value = rating.value / 10; // Normalize to 0-1
            return {
                x: centerX + radius * value * Math.cos(angle),
                y: centerY + radius * value * Math.sin(angle)
            };
        });

        ctx.beginPath();
        dataPoints.forEach((point, index) => {
            if (index === 0) ctx.moveTo(point.x, point.y);
            else ctx.lineTo(point.x, point.y);
        });
        ctx.closePath();
        ctx.strokeStyle = '#CCAA66';
        ctx.lineWidth = 1;
        ctx.stroke();
        ctx.fillStyle = '#CCAA66';
        ctx.fill();

        // Draw data points
        dataPoints.forEach(point => {
            ctx.beginPath();
            ctx.arc(point.x, point.y, 2, 0, 2 * Math.PI);
            ctx.fillStyle = '#CCAA66';
            ctx.fill();
        });
    }

    // Set canvas size with proper scaling
    function resizeCanvas() {
        const dpr = window.devicePixelRatio || 1;
        const rect = canvas.getBoundingClientRect();
        canvas.width = rect.width * dpr;
        canvas.height = rect.height * dpr;
        ctx.scale(dpr, dpr);
        canvas.style.width = rect.width + 'px';
        canvas.style.height = rect.height + 'px';
        drawChart();
    }

    // Initial draw
    resizeCanvas();

    // Redraw on window resize
    window.addEventListener('resize', resizeCanvas);
}); 