#!/usr/bin/env python3

import requests
import json
import sqlite3

def test_shot_deletion():
    # Login and get token
    login_data = {
        'email': 'admin@vfx.com',
        'password': 'admin123'
    }

    try:
        # Login to get token
        login_response = requests.post('http://localhost:8000/auth/login', json=login_data)
        print('Login status:', login_response.status_code)
        
        if login_response.status_code != 200:
            print('Login failed:', login_response.text)
            return
            
        token_data = login_response.json()
        access_token = token_data['access_token']
        headers = {'Authorization': 'Bearer ' + access_token}
        print('Login successful')
        
        # Create a test shot first
        shot_data = {
            'name': 'TEST_DELETION_DEBUG',
            'description': 'Test shot for deletion debugging',
            'frame_start': 1001,
            'frame_end': 1100
        }
        
        create_response = requests.post('http://localhost:8000/shots/?episode_id=1', json=shot_data, headers=headers)
        print('Create shot status:', create_response.status_code)
        
        if create_response.status_code != 201:
            print('Create failed:', create_response.text)
            return
            
        created_shot = create_response.json()
        shot_id = created_shot['id']
        print(f'Created shot with ID: {shot_id}')
        
        # Check database before deletion
        conn = sqlite3.connect('database.db')
        cursor = conn.cursor()
        cursor.execute('SELECT id, name, deleted_at, deleted_by FROM shots WHERE id = ?', (shot_id,))
        before_deletion = cursor.fetchone()
        print(f'Before deletion: ID={before_deletion[0]}, Name={before_deletion[1]}, Deleted_at={before_deletion[2]}, Deleted_by={before_deletion[3]}')
        
        # Now delete the shot
        delete_response = requests.delete(f'http://localhost:8000/shots/{shot_id}', headers=headers)
        print('Delete shot status:', delete_response.status_code)
        
        if delete_response.status_code == 200:
            delete_result = delete_response.json()
            print('Delete response:', json.dumps(delete_result, indent=2))
            
            # Check database after deletion
            cursor.execute('SELECT id, name, deleted_at, deleted_by FROM shots WHERE id = ?', (shot_id,))
            after_deletion = cursor.fetchone()
            print(f'After deletion: ID={after_deletion[0]}, Name={after_deletion[1]}, Deleted_at={after_deletion[2]}, Deleted_by={after_deletion[3]}')
        else:
            print('Delete failed:', delete_response.text)
            
        conn.close()
        
    except Exception as e:
        print('Error:', e)
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    test_shot_deletion()