const fs = require('fs');
const path = require('path');

function updateReadme(m3uPath) {
    // Get directories for both types of groups
    const countriesDir = path.join(path.dirname(m3uPath), 'countries');
    const culturalDir = path.join(path.dirname(m3uPath), 'cultural-groups');
    const groups = {
        countries: {},
        cultural: {}
    };
    
    // Read the main M3U to get original group names and channel counts
    const content = fs.readFileSync(m3uPath, 'utf8');
    const lines = content.split('\n');
    let totalChannels = 0;
    
    lines.forEach(line => {
        if (line.startsWith('#EXTINF')) {
            totalChannels++;
            const groupMatch = line.match(/group-title="([^"]*)"/);
            const groupTitle = groupMatch ? groupMatch[1] : 'Unknown';
            groups.countries[groupTitle] = (groups.countries[groupTitle] || 0) + 1;
        }
    });

    // Get cultural group counts
    if (fs.existsSync(culturalDir)) {
        fs.readdirSync(culturalDir)
            .filter(file => file.endsWith('.m3u'))
            .forEach(file => {
                const culturalContent = fs.readFileSync(path.join(culturalDir, file), 'utf8');
                const culturalLines = culturalContent.split('\n');
                const channelCount = culturalLines.filter(line => line.startsWith('#EXTINF')).length;
                const groupName = file.replace('.m3u', '');
                groups.cultural[groupName] = channelCount;
            });
    }

    // Build README content
    let readmeContent = '# Mystique\n\n';
    
    // Add description and features
    readmeContent += '## About\n\n';
    readmeContent += 'Mystique provides a curated collection of IPTV channels from around the world. ';
    readmeContent += 'The channels are organized by both countries and cultural groups for easy access.\n\n';

    // Add usage section
    readmeContent += '## Usage\n\n';
    readmeContent += '1. **Complete Playlist**: Use the main `mystique.m3u` file for access to all channels\n';
    readmeContent += '2. **Country-Specific**: Individual country playlists are available in the `countries/` directory\n';
    readmeContent += '3. **Cultural Groups**: Cultural/linguistic group playlists are available in the `cultural-groups/` directory\n\n';

    // Add statistics
    readmeContent += '## Statistics\n\n';
    readmeContent += `- Total Channels: ${totalChannels}\n`;
    readmeContent += `- Countries Available: ${Object.keys(groups.countries).length}\n`;
    readmeContent += `- Cultural Groups: ${Object.keys(groups.cultural).length}\n\n`;

    // Add playlists table
    readmeContent += '## Available Playlists\n\n';
    readmeContent += '| Playlist | Channels | Link |\n';
    readmeContent += '|----------|-----------|------|\n';
    
    // Add main playlist
    const mainPlaylistName = path.basename(m3uPath);
    readmeContent += `| **Complete (All channels)** | ${totalChannels} | [${mainPlaylistName}](${mainPlaylistName}) |\n`;
    
    // Add countries
    readmeContent += '| **───────── Countries ─────────** | | |\n';
    const sortedCountryGroups = Object.entries(groups.countries).sort((a, b) => a[0].localeCompare(b[0]));
    sortedCountryGroups.forEach(([groupName, channelCount]) => {
        const safeGroupName = groupName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
        readmeContent += `| ${groupName} | ${channelCount} | [${safeGroupName}.m3u](countries/${safeGroupName}.m3u) |\n`;
    });

    // Add cultural groups
    if (Object.keys(groups.cultural).length > 0) {
        readmeContent += '| **───────── Cultural Groups ─────────** | | |\n';
        const sortedCulturalGroups = Object.entries(groups.cultural).sort((a, b) => a[0].localeCompare(b[0]));
        sortedCulturalGroups.forEach(([groupName, channelCount]) => {
            const displayName = groupName
                .split('-')
                .map(word => word.charAt(0).toUpperCase() + word.slice(1))
                .join(' ');
            readmeContent += `| ${displayName} | ${channelCount} | [${groupName}.m3u](cultural-groups/${groupName}.m3u) |\n`;
        });
    }


    // Add note about legal usage
    readmeContent += '## Legal Notice\n\n';
    readmeContent += 'This playlist is a collection of publicly available IPTV streams. ';
    readmeContent += 'Please check your local laws regarding IPTV streaming before using this playlist.\n';
    
    const readmePath = path.join(path.dirname(m3uPath), 'README.md');
    fs.writeFileSync(readmePath, readmeContent);
    console.log('README.md has been updated with comprehensive playlist information');
}

const filePath = process.argv[2];
if (!filePath) {
    console.error('Please provide the path to mystique.m3u');
    process.exit(1);
}

try {
    updateReadme(filePath);
} catch (error) {
    console.error('Error updating README:', error.message);
    process.exit(1);
}