"""Script to fix email folders"""
import os
import sys
import django

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
django.setup()

from django.db import connection

cursor = connection.cursor()

acc_id = 'bb97f5aaf5e744049533fd1c84640084'
tenant_id = '7231bde9e867484faf5eef08ac2ac68d'

print('Fixing folders for account:', acc_id)

cursor.execute("DELETE FROM email_emailfolder WHERE id = 2")
print('Deleted duplicate INBOX (id=2)')

folders = [
    ('Sent', 'sent', 'sent'),
    ('Drafts', 'draft', 'draft'),
    ('Trash', 'trash', 'trash'),
    ('Spam', 'spam', 'spam'),
    ('Outbox', 'outbox', 'outbox'),
]

for name, ftype, remote_id in folders:
    cursor.execute(
        "INSERT INTO email_emailfolder (created_at, updated_at, tenant_id, name, remote_id, folder_type, unread_count, account_id) VALUES (datetime('now'), datetime('now'), %s, %s, %s, %s, 0, %s)",
        (tenant_id, name, remote_id, ftype, acc_id)
    )
    print('Created:', name)

cursor.execute('SELECT id, name, folder_type, account_id FROM email_emailfolder ORDER BY account_id, folder_type')
print()
print('All folders:')
for f in cursor.fetchall():
    print('  Account:', f[3], 'Name:', f[1], 'Type:', f[2])
