<?php
namespace App\Models;

use App\Core\Model;
use App\Services\EncryptionService;

class Fingerprint extends Model {
    protected $table = 'fingerprints';
    protected $primaryKey = 'id';
    protected $fillable = [
        'biometrics_id', 'finger_position', 'fingerprint_template', 
        'template_format', 'quality_score', 'capture_timestamp'
    ];
    
    protected $encryptionService;
    
    public function __construct() {
        parent::__construct();
        $this->encryptionService = new EncryptionService();
    }
    
    /**
     * Get fingerprints by biometrics ID
     */
    public function getByBiometricsId($biometrics_id) {
        $stmt = $this->db->prepare("
            SELECT * FROM {$this->table} 
            WHERE biometrics_id = ? 
            ORDER BY finger_position
        ");
        $stmt->execute([$biometrics_id]);
        return $stmt->fetchAll();
    }
    
    /**
     * Get fingerprints by person ID
     */
    public function getByPersonId($person_id) {
        $stmt = $this->db->prepare("
            SELECT f.* FROM {$this->table} f
            JOIN biometrics b ON f.biometrics_id = b.id
            WHERE b.person_id = ?
            ORDER BY b.capture_date DESC, f.finger_position
        ");
        $stmt->execute([$person_id]);
        return $stmt->fetchAll();
    }
    
    /**
     * Create fingerprint with encryption
     */
    public function create($data) {
        // Encrypt fingerprint template if provided
        if (isset($data['fingerprint_template']) && !empty($data['fingerprint_template'])) {
            $data['fingerprint_template'] = $this->encryptionService->encrypt($data['fingerprint_template']);
        }
        
        return parent::create($data);
    }
    
    /**
     * Get decrypted fingerprint template
     */
    public function getDecryptedTemplate($id) {
        $fp = $this->find($id);
        if ($fp && isset($fp['fingerprint_template'])) {
            $fp['fingerprint_template_decrypted'] = $this->encryptionService->decrypt($fp['fingerprint_template']);
        }
        return $fp;
    }
    
    /**
     * Get all fingerprints for a person with decryption
     */
    public function getDecryptedByPerson($person_id) {
        $fingerprints = $this->getByPersonId($person_id);
        
        foreach ($fingerprints as &$fp) {
            if (isset($fp['fingerprint_template'])) {
                $fp['fingerprint_template_decrypted'] = $this->encryptionService->decrypt($fp['fingerprint_template']);
            }
        }
        
        return $fingerprints;
    }
    
    /**
     * Check if person has all required fingerprints
     */
    public function hasCompleteSet($person_id) {
        $required = [
            'Right Thumb', 'Right Index', 'Left Thumb', 'Left Index'
        ];
        
        $fingerprints = $this->getByPersonId($person_id);
        $existing = array_column($fingerprints, 'finger_position');
        
        $missing = array_diff($required, $existing);
        
        return [
            'complete' => empty($missing),
            'missing' => $missing,
            'total' => count($fingerprints),
            'required_count' => count($required)
        ];
    }
}