import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

import psycopg2

conn = psycopg2.connect(host='192.168.1.76', port=5432, dbname='fotky_buzalkovi',
                        user='vladimir.buzalka', password='Vlado7309208104++')
cur = conn.cursor()

# 1. Duplikáty sha256_pixels
print('=== DUPLIKÁTY sha256_pixels ===')
cur.execute('''SELECT sha256_pixels, COUNT(*) as cnt FROM photos
    WHERE sha256_pixels IS NOT NULL GROUP BY sha256_pixels HAVING COUNT(*) > 1
    ORDER BY cnt DESC LIMIT 10''')
for r in cur.fetchall():
    print(f'  {r[0][:16]}... : {r[1]}x')
cur.execute('''SELECT COUNT(*), SUM(cnt) FROM (
    SELECT COUNT(*) as cnt FROM photos WHERE sha256_pixels IS NOT NULL
    GROUP BY sha256_pixels HAVING COUNT(*) > 1) x''')
r = cur.fetchone()
print(f'Celkem skupin duplikátů: {r[0]}, fotek v duplikátech: {r[1]}')

# 2. Identické phash
print('\n=== IDENTICKÉ phash ===')
cur.execute('''SELECT phash, COUNT(*) as cnt FROM photos
    WHERE phash IS NOT NULL GROUP BY phash HAVING COUNT(*) > 1
    ORDER BY cnt DESC LIMIT 10''')
for r in cur.fetchall():
    print(f'  phash={r[0]}: {r[1]}x')
cur.execute('''SELECT COUNT(*), SUM(cnt) FROM (
    SELECT COUNT(*) as cnt FROM photos WHERE phash IS NOT NULL
    GROUP BY phash HAVING COUNT(*) > 1) x''')
r = cur.fetchone()
print(f'Celkem skupin: {r[0]}, fotek: {r[1]}')

# 3. Screenshoty
print('\n=== SCREENSHOTY ===')
cur.execute('SELECT COUNT(*) FROM photos WHERE is_screenshot = true')
print(f'is_screenshot=true: {cur.fetchone()[0]}')

# 4. Objektivy
print('\n=== TOP OBJEKTIVY ===')
cur.execute('''SELECT COALESCE(lens_model, '(neuvedeno)') as lens, COUNT(*) as cnt
    FROM photos GROUP BY lens ORDER BY cnt DESC LIMIT 15''')
for r in cur.fetchall():
    print(f'  {r[1]:>6}  {r[0]}')

# 5. ISO distribuce
print('\n=== ISO DISTRIBUCE ===')
cur.execute('''SELECT iso, COUNT(*) as cnt FROM photos WHERE iso IS NOT NULL
    GROUP BY iso ORDER BY cnt DESC LIMIT 15''')
for r in cur.fetchall():
    print(f'  ISO {r[0]:>6}: {r[1]}')

# 6. Clona
print('\n=== CLONA (aperture) TOP ===')
cur.execute('''SELECT aperture, COUNT(*) as cnt FROM photos WHERE aperture IS NOT NULL
    GROUP BY aperture ORDER BY cnt DESC LIMIT 15''')
for r in cur.fetchall():
    print(f'  f/{r[0]}: {r[1]}')

# 7. Expoziční čas
print('\n=== EXPOZIČNÍ ČAS TOP ===')
cur.execute('''SELECT exposure_time, COUNT(*) as cnt FROM photos WHERE exposure_time IS NOT NULL
    GROUP BY exposure_time ORDER BY cnt DESC LIMIT 15''')
for r in cur.fetchall():
    print(f'  {r[0]}: {r[1]}')

# 8. GPS top lokace
print('\n=== GPS TOP LOKACE (zaokrouhleno na 0.1 stupne) ===')
cur.execute('''SELECT ROUND(gps_lat::numeric, 1) as lat, ROUND(gps_lon::numeric, 1) as lon, COUNT(*) as cnt
    FROM photos WHERE gps_lat IS NOT NULL AND gps_lon IS NOT NULL
    GROUP BY lat, lon ORDER BY cnt DESC LIMIT 20''')
for r in cur.fetchall():
    print(f'  [{r[0]}, {r[1]}]: {r[2]} fotek')

cur.execute('''SELECT MIN(gps_lat), MAX(gps_lat), MIN(gps_lon), MAX(gps_lon)
    FROM photos WHERE gps_lat IS NOT NULL''')
r = cur.fetchone()
print(f'  Rozsah Lat: {r[0]} .. {r[1]}')
print(f'  Rozsah Lon: {r[2]} .. {r[3]}')

# 9. Megapixely po letech
print('\n=== PRUMERNE MEGAPIXELY PO LETECH ===')
cur.execute('''SELECT EXTRACT(YEAR FROM taken_at)::INT as rok,
    ROUND(AVG(megapixels)::numeric, 1) as avg_mp,
    ROUND(MAX(megapixels)::numeric, 1) as max_mp, COUNT(*) as cnt
    FROM photos WHERE taken_at IS NOT NULL AND megapixels IS NOT NULL
    GROUP BY rok ORDER BY rok''')
for r in cur.fetchall():
    print(f'  {r[0]}: avg={r[1]} MP, max={r[2]} MP ({r[3]} fotek)')

# 10. Formáty
print('\n=== FORMATY ===')
cur.execute('''SELECT COALESCE(file_ext, '(none)') as ext, COUNT(*) as cnt
    FROM photos GROUP BY ext ORDER BY cnt DESC''')
for r in cur.fetchall():
    print(f'  {r[0]}: {r[1]}')

# 11. Průhlednost
print('\n=== PRUHLEDNOST ===')
cur.execute('SELECT COUNT(*) FROM photos WHERE has_transparency = true')
print(f'  S pruhlednosti: {cur.fetchone()[0]}')

# 12. Barevné módy
print('\n=== BAREVNE MODY ===')
cur.execute('''SELECT COALESCE(mode, '(none)') as m, COUNT(*) as cnt
    FROM photos GROUP BY m ORDER BY cnt DESC''')
for r in cur.fetchall():
    print(f'  {r[0]}: {r[1]}')

# 13. Neznámé fotky - vzory názvů
print('\n=== NEZNAME FOTKY 2015-2016 (bez kamery) - vzory nazvu ===')
cur.execute('''SELECT file_name FROM photos
    WHERE camera_model IS NULL AND EXTRACT(YEAR FROM taken_at) BETWEEN 2015 AND 2016
    LIMIT 30''')
for r in cur.fetchall():
    print(f'  {r[0]}')

print('\n=== NEZNAME FOTKY 2022 - vzory nazvu ===')
cur.execute('''SELECT file_name FROM photos
    WHERE camera_model IS NULL AND EXTRACT(YEAR FROM taken_at) = 2022
    LIMIT 30''')
for r in cur.fetchall():
    print(f'  {r[0]}')

print('\n=== 2022 PREFIXES ===')
cur.execute('''SELECT LEFT(file_name, 10) as prefix, COUNT(*) as cnt FROM photos
    WHERE camera_model IS NULL AND EXTRACT(YEAR FROM taken_at) = 2022
    GROUP BY prefix ORDER BY cnt DESC LIMIT 15''')
for r in cur.fetchall():
    print(f'  {r[0]}: {r[1]}')

# 14. Měsíce
print('\n=== FOTKY PO MESICICH ===')
nazvy = ['','Leden','Unor','Brezen','Duben','Kveten','Cerven','Cervenec','Srpen','Zari','Rijen','Listopad','Prosinec']
cur.execute('''SELECT EXTRACT(MONTH FROM taken_at)::INT as mesic, COUNT(*) as cnt
    FROM photos WHERE taken_at IS NOT NULL GROUP BY mesic ORDER BY mesic''')
for r in cur.fetchall():
    print(f'  {nazvy[r[0]]}: {r[1]}')

# 15. Dny v týdnu
print('\n=== FOTKY PO DNECH V TYDNU ===')
dny = ['Nedele','Pondeli','Utery','Streda','Ctvrtek','Patek','Sobota']
cur.execute('''SELECT EXTRACT(DOW FROM taken_at)::INT as den, COUNT(*) as cnt
    FROM photos WHERE taken_at IS NOT NULL GROUP BY den ORDER BY den''')
for r in cur.fetchall():
    print(f'  {dny[r[0]]}: {r[1]}')

# 16. Hodiny
print('\n=== FOTKY PO HODINACH ===')
cur.execute('''SELECT EXTRACT(HOUR FROM taken_at)::INT as hod, COUNT(*) as cnt
    FROM photos WHERE taken_at IS NOT NULL GROUP BY hod ORDER BY hod''')
for r in cur.fetchall():
    print(f'  {r[0]:02d}:00 - {r[1]}')

# 17. Top dny (události)
print('\n=== TOP 20 DNU (nejvic fotek = udalosti) ===')
cur.execute('''SELECT taken_at::date as den, COUNT(*) as cnt
    FROM photos WHERE taken_at IS NOT NULL GROUP BY den ORDER BY cnt DESC LIMIT 20''')
for r in cur.fetchall():
    print(f'  {r[0]}: {r[1]} fotek')

conn.close()
