from flask import Blueprint, jsonify, session
from db import query_db, execute_db
from utils import login_required

admin_locations_bp = Blueprint('admin_locations', __name__)

@admin_locations_bp.route('/location/<int:location_count_id>/reopen', methods=['POST'])
@login_required
def reopen_location(location_count_id):
    """Reopen a completed location (admin/owner only)"""
    # Check permissions
    user = query_db('SELECT role FROM Users WHERE user_id = ?', [session['user_id']], one=True)
    if not user or user['role'] not in ['owner', 'admin']:
        return jsonify({'success': False, 'message': 'Permission denied'}), 403
    
    # Verify location exists
    loc = query_db('SELECT * FROM LocationCounts WHERE location_count_id = ?', [location_count_id], one=True)
    
    if not loc:
        return jsonify({'success': False, 'message': 'Location not found'})
    
    # Reopen the location
    execute_db('''
        UPDATE LocationCounts
        SET status = 'in_progress', end_timestamp = NULL
        WHERE location_count_id = ?
    ''', [location_count_id])
    
    return jsonify({'success': True, 'message': 'Bin reopened for counting'})


@admin_locations_bp.route('/location/<int:location_count_id>/scans')
@login_required
def get_location_scans(location_count_id):
    """Get all scans for a specific location (admin/owner only)"""
    # Check permissions
    user = query_db('SELECT role FROM Users WHERE user_id = ?', [session['user_id']], one=True)
    if not user or user['role'] not in ['owner', 'admin']:
        return jsonify({'success': False, 'message': 'Permission denied'}), 403
    
    try:
        scans = query_db('''
            SELECT 
                se.*,
                bic.system_bin as current_system_location,
                bic.system_quantity as current_system_weight
            FROM ScanEntries se
            LEFT JOIN BaselineInventory_Current bic ON se.lot_number = bic.lot_number
            WHERE se.location_count_id = ?
            AND se.is_deleted = 0
            ORDER BY se.scan_timestamp DESC
        ''', [location_count_id])
        
        # Convert Row objects to dicts
        scans_list = [dict(scan) for scan in scans] if scans else []
        
        return jsonify({'success': True, 'scans': scans_list})
    
    except Exception as e:
        return jsonify({'success': False, 'message': str(e)})
    
@admin_locations_bp.route('/location/<int:location_count_id>/delete', methods=['POST'])
@login_required
def soft_delete_location(location_count_id):
    """Admin-only: Soft delete a bin count and its associated data"""
    if session.get('role') not in ['owner', 'admin']:
        return jsonify({'success': False, 'message': 'Admin role required'}), 403

    # 1. Verify location exists
    loc = query_db('SELECT session_id, location_name FROM LocationCounts WHERE location_count_id = ?', 
                   [location_count_id], one=True)
    
    if not loc:
        return jsonify({'success': False, 'message': 'Location not found'})
    
    # 2. Soft delete the bin count itself
    execute_db('''
        UPDATE LocationCounts 
        SET is_deleted = 1 
        WHERE location_count_id = ?
    ''', [location_count_id])
    
    # 3. Soft delete all scans in that bin
    execute_db('''
        UPDATE ScanEntries 
        SET is_deleted = 1 
        WHERE location_count_id = ?
    ''', [location_count_id])
    
    # 4. Remove any MissingLots records generated for this bin
    execute_db('''
        DELETE FROM MissingLots 
        WHERE session_id = ? AND master_expected_location = ?
    ''', [loc['session_id'], loc['location_name']])
    
    return jsonify({'success': True, 'message': 'Bin count and associated data soft-deleted'})