-- ============================================
-- Script d'exploration des duplications de calendriers et événements
-- ============================================

-- 1. Lister tous les calendriers "Privée" avec leurs détails
SELECT 
    c.id,
    c.name,
    c.color,
    c.description,
    c."userId",
    c."missionId",
    c."createdAt",
    c."updatedAt",
    cs.id as "syncConfigId",
    cs.provider,
    cs."externalCalendarId",
    cs."syncEnabled",
    cs."lastSyncAt",
    mc.email as "mailCredentialEmail",
    (SELECT COUNT(*) FROM "Event" e WHERE e."calendarId" = c.id) as "eventCount"
FROM "Calendar" c
LEFT JOIN "CalendarSync" cs ON cs."calendarId" = c.id
LEFT JOIN "MailCredentials" mc ON mc.id = cs."mailCredentialId"
WHERE c.name = 'Privée'
ORDER BY c."createdAt" DESC;

-- 2. Voir tous les événements "Vendredi saint" avec leurs calendriers
SELECT 
    e.id,
    e.title,
    e."calendarId",
    c.name as "calendarName",
    e."externalEventId",
    e.start,
    e.end,
    e."isAllDay",
    e."createdAt",
    e."updatedAt"
FROM "Event" e
JOIN "Calendar" c ON c.id = e."calendarId"
WHERE e.title = 'Vendredi saint'
ORDER BY e.start, c.name;

-- 3. Identifier les événements en double par externalEventId (même événement dans plusieurs calendriers)
SELECT 
    e."externalEventId",
    e.title,
    COUNT(*) as "occurrenceCount",
    STRING_AGG(DISTINCT c.id::text, ', ') as "calendarIds",
    STRING_AGG(DISTINCT c.name, ', ') as "calendarNames",
    MIN(e.start) as "earliestStart",
    MAX(e.start) as "latestStart"
FROM "Event" e
JOIN "Calendar" c ON c.id = e."calendarId"
WHERE e."externalEventId" IS NOT NULL
  AND e."externalEventId" != ''
GROUP BY e."externalEventId", e.title
HAVING COUNT(*) > 1
ORDER BY "occurrenceCount" DESC, e.title;

-- 4. Voir les détails complets des événements en double
SELECT 
    e.id,
    e.title,
    e."externalEventId",
    e."calendarId",
    c.name as "calendarName",
    c."userId",
    e.start,
    e.end,
    e."isAllDay",
    e."createdAt",
    e."updatedAt"
FROM "Event" e
JOIN "Calendar" c ON c.id = e."calendarId"
WHERE e."externalEventId" IN (
    SELECT "externalEventId"
    FROM "Event"
    WHERE "externalEventId" IS NOT NULL
      AND "externalEventId" != ''
    GROUP BY "externalEventId"
    HAVING COUNT(*) > 1
)
ORDER BY e."externalEventId", e."calendarId", e.start;

-- 5. Compter les événements par calendrier "Privée"
SELECT 
    c.id as "calendarId",
    c.name,
    COUNT(e.id) as "totalEvents",
    COUNT(CASE WHEN e."externalEventId" IS NOT NULL THEN 1 END) as "eventsWithExternalId",
    COUNT(CASE WHEN e."externalEventId" IS NULL THEN 1 END) as "eventsWithoutExternalId"
FROM "Calendar" c
LEFT JOIN "Event" e ON e."calendarId" = c.id
WHERE c.name = 'Privée'
GROUP BY c.id, c.name
ORDER BY "totalEvents" DESC;

-- 6. Voir les événements "TEST de connaissances" (celui qui apparaît en double dans les logs)
SELECT 
    e.id,
    e.title,
    e."externalEventId",
    e."calendarId",
    c.name as "calendarName",
    e.start,
    e.end,
    e."createdAt"
FROM "Event" e
JOIN "Calendar" c ON c.id = e."calendarId"
WHERE e.title LIKE '%TEST de connaissances%'
   OR e."externalEventId" = 'AAMkADg2NjBjMDUwLTJlMzYtNDBlZi1iNzE4LTA5YWFiYTdhMWJmYwBGAAAAAABiHJmWOydNSY0OtAePspbBBwBWDBinTymARZbnqCRYQq7IAAAAAAENAABWDBinTymARZbnqCRYQq7IAAMGtqjeAAA='
ORDER BY e."calendarId", e.start;

-- 7. Lister tous les CalendarSync pour les calendriers "Privée"
SELECT 
    cs.id,
    cs."calendarId",
    c.name as "calendarName",
    cs.provider,
    cs."externalCalendarId",
    cs."syncEnabled",
    cs."lastSyncAt",
    cs."syncFrequency",
    cs."lastSyncError",
    mc.email as "mailCredentialEmail"
FROM "CalendarSync" cs
JOIN "Calendar" c ON c.id = cs."calendarId"
LEFT JOIN "MailCredentials" mc ON mc.id = cs."mailCredentialId"
WHERE c.name = 'Privée'
ORDER BY cs."createdAt" DESC;

-- 8. Trouver les événements sans externalEventId dans les calendriers Microsoft
SELECT 
    e.id,
    e.title,
    e."calendarId",
    c.name as "calendarName",
    e.start,
    e.end,
    e."createdAt",
    cs.provider
FROM "Event" e
JOIN "Calendar" c ON c.id = e."calendarId"
LEFT JOIN "CalendarSync" cs ON cs."calendarId" = c.id
WHERE c.name = 'Privée'
  AND (e."externalEventId" IS NULL OR e."externalEventId" = '')
  AND cs.provider = 'microsoft'
ORDER BY e.start DESC;
