import sys

with open('index.php', 'r') as f:
    lines = f.readlines()

start_marker = "<?php foreach($messages as $m):"
# We want the one for normal channels, which is the second occurrence (first is for threads)
occurrences = [i for i, line in enumerate(lines) if start_marker in line]
if len(occurrences) < 2:
    print(f"Error: Found only {len(occurrences)} occurrences of start marker")
    sys.exit(1)

# The one we want to fix is usually the last one if I've been duplicating it
# But let's be careful. The thread one is inside if($active_thread)
# The normal one is inside the final else.

# Let's find the main else at line 1271
else_line = 0
for i, line in enumerate(lines):
    if "<?php else: ?>" in line and i > 1200: # around there
        else_line = i
        break

if not else_line:
    print("Error: Could not find the final else line")
    sys.exit(1)

# Now find the first foreach after this else
start_line = 0
for i in range(else_line, len(lines)):
    if start_marker in lines[i]:
        start_line = i
        break

if not start_line:
    print("Error: Could not find start marker after else")
    sys.exit(1)

# Now find the end of this block. It should end with <?php endforeach; ?>
# But I might have multiple ones now.
# We want to find the one that is followed by <?php endif; ?> (closing the main if)
# and then </div> (closing chat-container or messages-list)

end_line = 0
for i in range(start_line, len(lines)):
    if "<?php endforeach; ?>" in lines[i]:
        # Check if next line (or soon after) is <?php endif; ?>
        is_real_end = False
        for j in range(i+1, min(i+10, len(lines))):
            if "<?php endif; ?>" in lines[j]:
                is_real_end = True
                end_line = j
                break
        if is_real_end:
            break

if not end_line:
    print("Error: Could not find end marker")
    sys.exit(1)

print(f"Replacing lines {start_line+1} to {end_line+1}")

new_block = """                <?php foreach($messages as $m): 
                    $mention_pattern = '/@' . preg_quote($user['username'], '/') . '\\b/';
                    $is_mentioned = preg_match($mention_pattern, $m['content']);
                ?>
                    <div class="message-item <?php echo $is_mentioned ? 'mentioned' : ''; ?> <?php echo $m['is_pinned'] ? 'pinned' : ''; ?> <?php echo $channel_type === 'announcement' ? 'announcement-style' : ''; ?>" data-id="<?php echo $m['id']; ?>" data-raw-content="<?php echo htmlspecialchars($m['content']); ?>">
                        <div class="message-avatar" style="<?php echo $m['avatar_url'] ? "background-image: url('{$m['avatar_url']}');" : ""; ?>"></div>
                        <div class="message-content">
                            <div class="message-header">
                                <span class="message-author" style="<?php echo !empty($m['role_color']) ? "color: {$m['role_color']};" : ""; ?>">
                                    <?php echo htmlspecialchars($m['username']); ?>
                                    <?php echo renderRoleIcon($m['role_icon'], '14px'); ?>
                                </span>
                                <span class="message-time"><?php echo date('H:i', strtotime($m['created_at'])); ?></span>
                                <?php if ($m['is_pinned']): ?>
                                    <span class="pinned-badge ms-2" title="Message épinglé">
                                        <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path></svg>
                                        Épinglé
                                    </span>
                                <?php endif; ?>
                            </div>
                            <div class="message-text">
                                <?php 
                                    $is_manual_ann = false;
                                    $is_poll = false;
                                    if (!empty($m['metadata'])) {
                                        $meta_check = json_decode($m['metadata'], true);
                                        if (isset($meta_check['is_manual_announcement']) && $meta_check['is_manual_announcement']) {
                                            $is_manual_ann = true;
                                        }
                                        if (isset($meta_check['is_poll']) && $meta_check['is_poll']) {
                                            $is_poll = true;
                                        }
                                    }
                                    if (!$is_manual_ann && !$is_poll) {
                                        echo parse_emotes($m['content'], $user['username']); 
                                    }
                                ?>
                                <?php if ($m['attachment_url']): ?>
                                    <div class="message-attachment mt-2">
                                        <?php 
                                            $ext = strtolower(pathinfo($m['attachment_url'], PATHINFO_EXTENSION));
                                            if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])):
                                        ?>
                                            <img src="<?php echo htmlspecialchars($m['attachment_url']); ?>" class="img-fluid rounded message-img-preview" alt="Attachment" style="max-height: 300px; cursor: pointer;" onclick="window.open(this.src)">
                                        <?php else: ?>
                                            <a href="<?php echo htmlspecialchars($m['attachment_url']); ?>" target="_blank" class="attachment-link d-inline-flex align-items-center p-2 rounded bg-dark text-white text-decoration-none">
                                                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-2"><path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline></svg>
                                                <?php echo basename($m['attachment_url']); ?>
                                            </a>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
    
                                <?php if (!empty($m['metadata'])): 
                                    $meta = json_decode($m['metadata'], true);
                                    if ($meta): 
                                        $borderColor = htmlspecialchars($meta['color'] ?? 'var(--blurple)');
                                        if (!empty($meta['is_poll'])):
                                ?>
                                    <div class="poll-container rich-embed mt-2 p-3 rounded" style="border-left: 4px solid <?php echo $borderColor; ?>;">
                                        <div class="embed-site-name mb-1" style="font-size: 0.75em; color: var(--text-muted); text-transform: uppercase; font-weight: bold;">📊 SONDAGE</div>
                                        <?php if (!empty($meta['title'])): ?>
                                            <div class="embed-title d-block mb-1" style="font-weight: 600; color: #00a8fc; font-size: 1.1em;"><?php echo htmlspecialchars($meta['title']); ?></div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['description'])): ?>
                                            <div class="embed-description mb-2" style="font-size: 0.9em; color: var(--text-normal); max-height: 100px; overflow: hidden; position: relative;"><?php echo parse_markdown($meta['description']); ?></div>
                                            <div class="read-more-btn small mb-2" style="display: none; cursor: pointer; color: var(--blurple); font-weight: 600;">Voir plus</div>
                                        <?php endif; ?>
                                        
                                        <div class="poll-options-list">
                                            <?php 
                                            $total_votes = 0;
                                            if (isset($m['votes_data'])) {
                                                foreach($m['votes_data'] as $v) $total_votes += (int)$v['vote_count'];
                                            }
                                            $options = $meta['options'] ?? [];
                                            foreach($options as $idx => $opt): 
                                                $count = 0;
                                                $user_voted = false;
                                                if (isset($m['votes_data'])) {
                                                    foreach($m['votes_data'] as $v) {
                                                        if ($v['option_index'] == $idx) {
                                                            $count = (int)$v['vote_count'];
                                                            $user_ids = explode(',', $v['user_ids'] ?? '');
                                                            if (in_array($current_user_id, $user_ids)) {
                                                                $user_voted = true;
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                $percent = $total_votes > 0 ? round(($count / $total_votes) * 100) : 0;
                                                $is_expired = !empty($meta['end_date']) && strtotime($meta['end_date']) < time();
                                            ?>
                                                <div class="poll-option <?php echo $user_voted ? 'voted' : ''; ?> <?php echo $is_expired ? 'expired' : ''; ?>" data-message-id="<?php echo $m['id']; ?>" data-option-index="<?php echo $idx; ?>">
                                                    <div class="poll-progress-container">
                                                        <div class="poll-progress-bar" style="width: <?php echo $percent; ?>%"></div>
                                                        <span class="poll-option-text"><?php echo htmlspecialchars($opt); ?></span>
                                                        <span class="poll-option-percent"><?php echo $percent; ?>% (<?php echo $count; ?>)</span>
                                                    </div>
                                                </div>
                                            <?php endforeach; ?>
                                        </div>
                                        <div class="poll-footer mt-2" style="font-size: 0.75em; color: var(--text-muted); display: flex; justify-content: space-between; align-items: center;">
                                            <span><?php echo $total_votes; ?> vote<?php echo $total_votes > 1 ? 's' : ''; ?> · <?php echo ($meta['choice_type'] ?? 'single') === 'multiple' ? 'Plusieurs choix' : 'Choix unique'; ?></span>
                                            <?php if (!empty($meta['end_date'])): ?>
                                                <span class="poll-deadline <?php echo $is_expired ? 'text-danger' : ''; ?>">
                                                    <?php echo $is_expired ? 'Terminé' : 'Expire le ' . date('d/m/Y H:i', strtotime($meta['end_date'])); ?>
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <?php else: ?>
                                    <div class="rich-embed mt-2 p-3 rounded" style="background: rgba(0,0,0,0.1); border-left: 4px solid <?php echo $borderColor; ?>; max-width: 520px;">
                                        <?php if (!empty($meta['site_name']) && empty($meta['is_rss']) && empty($meta['is_manual_announcement'])): ?>
                                            <div class="embed-site-name mb-1" style="font-size: 0.75em; color: var(--text-muted); text-transform: uppercase; font-weight: bold;"><?php echo htmlspecialchars($meta['site_name']); ?></div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['title'])): ?>
                                            <?php if (!empty($meta['url'])): ?>
                                                <a href="<?php echo htmlspecialchars($meta['url']); ?>" target="_blank" class="embed-title d-block mb-1 text-decoration-none" style="font-weight: 600; color: #00a8fc; font-size: 1.1em;"><?php echo htmlspecialchars($meta['title']); ?></a>
                                            <?php else: ?>
                                                <div class="embed-title d-block mb-1" style="font-weight: 600; color: #00a8fc; font-size: 1.1em;"><?php echo htmlspecialchars($meta['title']); ?></div>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['is_rss'])): ?>
                                            <div class="embed-meta mb-2" style="font-size: 0.8em; color: var(--text-muted);">
                                                <?php 
                                                    $parts = [];
                                                    if (!empty($meta['category'])) $parts[] = htmlspecialchars($meta['category']);
                                                    if (!empty($meta['date'])) $parts[] = htmlspecialchars($meta['date']);
                                                    if (!empty($meta['author'])) $parts[] = htmlspecialchars($meta['author']);
                                                    echo implode(' · ', $parts);
                                                ?>
                                            </div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['is_manual_announcement'])): ?>
                                            <div class="embed-meta mb-2" style="font-size: 0.8em; color: var(--text-muted);">
                                                <?php echo date('d/m/Y H:i', strtotime($m['created_at'])); ?>
                                            </div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['description'])): ?>
                                            <div class="embed-description mb-2" style="font-size: 0.9em; color: var(--text-normal); max-height: 100px; overflow: hidden; position: relative;"><?php echo parse_markdown($meta['description']); ?></div>
                                            <div class="read-more-btn small mb-2" style="display: none; cursor: pointer; color: var(--blurple); font-weight: 600;">Voir plus</div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['is_manual_announcement']) && !empty($meta['url'])): ?>
                                            <div class="embed-footer mt-2">
                                                <a href="<?php echo htmlspecialchars($meta['url']); ?>" target="_blank" class="btn btn-sm btn-outline-info" style="font-size: 0.8em;">Source / En savoir plus</a>
                                            </div>
                                        <?php endif; ?>
                                        <?php if (!empty($meta['image'])): ?>
                                            <div class="embed-image">
                                                <img src="<?php echo htmlspecialchars($meta['image']); ?>" class="rounded" style="max-width: 100%; max-height: 300px; object-fit: contain;">
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <?php endif; ?>
                                <?php endif; endif; ?>
                            </div>
                            <div class="message-reactions mt-1" data-message-id="<?php echo $m['id']; ?>">
                                <?php
                                // Fetch reactions for this message
                                $stmt_react = db()->prepare("SELECT emoji, COUNT(*) as count, GROUP_CONCAT(user_id) as users FROM message_reactions WHERE message_id = ? GROUP BY emoji");
                                $stmt_react->execute([$m['id']]);
                                $reactions = $stmt_react->fetchAll();
                                foreach ($reactions as $r):
                                    $reacted = in_array($current_user_id, explode(',', $r['users']));
                                ?>
                                    <span class="reaction-badge <?php echo $reacted ? 'active' : ''; ?>" data-emoji="<?php echo htmlspecialchars($r['emoji']); ?>">
                                        <?php echo parse_emotes($r['emoji']); ?> <span class="count"><?php echo $r['count']; ?></span>
                                    </span>
                                <?php endforeach; ?>
                                <span class="add-reaction-btn" title="Ajouter une réaction">+</span>
                            </div>
                        </div>
                        <?php if ($m['user_id'] == $current_user_id || ($active_server_id != 'dms' && $can_manage_channels)): ?>
                            <div class="message-actions-menu">
                                <span class="action-btn pin <?php echo $m['is_pinned'] ? 'active' : ''; ?>" title="<?php echo $m['is_pinned'] ? 'Désépingler' : 'Épingler'; ?>" data-id="<?php echo $m['id']; ?>" data-pinned="<?php echo $m['is_pinned'] ? '1' : '0'; ?>">
                                    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path><circle cx="12" cy="10" r="3"></circle></svg>
                                </span>
                                <?php if ($m['user_id'] == $current_user_id || ($is_manual_ann && $can_manage_channels)): ?>
                                    <span class="action-btn edit <?php echo $is_manual_ann ? 'edit-announcement' : ''; ?>" title="Modifier" data-id="<?php echo $m['id']; ?>">
                                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path><path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path></svg>
                                    </span>
                                    <span class="action-btn delete" title="Supprimer" data-id="<?php echo $m['id']; ?>">
                                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>
                                    </span>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>"""

# Adjust end_line to be the last endif before the start of next section
# The original file has <?php endif; ?> at line 1486 in my previous read.
# Let's verify.

with open('index.php', 'w') as f:
    f.writelines(lines[:start_line])
    f.write(new_block)
    f.write("\n")
    f.writelines(lines[end_line+1:])

