<?php
// Define paths for images
$relative_image_dir "assets/images/movies/";
$default_image $relative_image_dir "default-movie.jpg";

// Use an existing directory for KDM processing
$kdm_temp_dir "assets/kdm/";

// Format duration function
function formatDuration($minutes) {
    
$hours floor($minutes 60);
    
$mins $minutes 60;
    return (
$hours $hours 'h ' '') . $mins 'm';
}

// Check if a film is currently showing (has valid KDM dates)
function isCurrentlyShowing($kdmStart$kdmEnd) {
    if (empty(
$kdmStart) || empty($kdmEnd)) {
        return 
false;
    }
    
    
$now = new DateTime();
    
$start = new DateTime($kdmStart);
    
$end = new DateTime($kdmEnd);
    
    return 
$now >= $start && $now <= $end;
}

// Process KDM file and extract dates
function processKdmFile($kdm_file$extract_dir) {
    
$zip = new ZipArchive();
    if (
$zip->open($kdm_file) === TRUE) {
        
$zip->extractTo($extract_dir);
        
$zip->close();
        
        
// Look for XML files in the extracted directory
        
$xml_files glob($extract_dir '/*.xml');
        
        if (
count($xml_files) > 0) {
            
// Process the first XML file found
            
$xml_content file_get_contents($xml_files[0]);
            
            
// Parse XML to find KDM dates
            
$kdm_start null;
            
$kdm_end null;
            
            if (
preg_match('/<ContentKeysNotValidBefore>(.*?)<\/ContentKeysNotValidBefore>/'$xml_content$start_matches)) {
                
$kdm_start $start_matches[1];
            }
            
            if (
preg_match('/<ContentKeysNotValidAfter>(.*?)<\/ContentKeysNotValidAfter>/'$xml_content$end_matches)) {
                
$kdm_end $end_matches[1];
            }
            
            
// If dates were found, return them
            
if ($kdm_start && $kdm_end) {
                
// Convert to MySQL datetime format
                
$kdm_start_mysql date('Y-m-d H:i:s'strtotime($kdm_start));
                
$kdm_end_mysql date('Y-m-d H:i:s'strtotime($kdm_end));
                
                return [
                    
'start' => $kdm_start_mysql,
                    
'end' => $kdm_end_mysql
                
];
            }
        }
    }
    
    return 
false;
}

// Clean up KDM temporary directory
function cleanupKdmDirectory($extract_dir$kdm_temp_dir) {
    
// Clean up the temporary directory - only if it's not the main kdm directory
    
if ($extract_dir !== $kdm_temp_dir) {
        
// Only try to remove files and directory if we have permission
        
@array_map('unlink'glob($extract_dir "/*"));
        @
rmdir($extract_dir);
    } else {
        
// Just remove the XML files if we're using the main directory
        
@array_map('unlink'glob($extract_dir "/*.xml"));
    }
}

// Check if a film can be deleted
function canDeleteFilm($dbc$film_id) {
    
$checkStmt $dbc->prepare("SELECT COUNT(*) as count FROM CINE_Plage WHERE fk_Film = ?");
    
$checkStmt->bind_param('i'$film_id);
    
$checkStmt->execute();
    
$checkResult $checkStmt->get_result();
    
$usageCount $checkResult->fetch_assoc()['count'];
    
$checkStmt->close();
    
    return 
$usageCount === 0;
}