<?php
// File: includes/class-snippet.php

if (!defined('ABSPATH')) {
    exit;
}

class PC_HFAP_Snippet {
    
    private $id;
    private $title;
    private $location;
    private $code;
    private $created_at;
    private $updated_at;
    
    public function __construct($data = array()) {
        if (!empty($data)) {
            $this->id = isset($data['id']) ? intval($data['id']) : null;
            $this->title = isset($data['title']) ? sanitize_text_field($data['title']) : '';
            $this->location = isset($data['location']) && in_array($data['location'], array('header', 'footer', 'body')) 
                ? $data['location'] 
                : 'header';
            // Store code as-is, don't apply wp_unslash() here
            $this->code = isset($data['code']) ? $data['code'] : '';
            $this->created_at = isset($data['created_at']) ? $data['created_at'] : current_time('mysql');
            $this->updated_at = isset($data['updated_at']) ? $data['updated_at'] : current_time('mysql');
        }
    }
    
    public function save() {
        if ($this->id) {
            // Update existing snippet
            $data = array(
                'title' => $this->title,
                'location' => $this->location,
                'code' => $this->code
            );
            return PC_HFAP_Database::update_snippet($this->id, $data);
        } else {
            // Insert new snippet
            $data = array(
                'title' => $this->title,
                'location' => $this->location,
                'code' => $this->code
            );
            $this->id = PC_HFAP_Database::insert_snippet($data);
            return $this->id;
        }
    }
    
    public function delete() {
        if ($this->id) {
            return PC_HFAP_Database::delete_snippet($this->id);
        }
        return false;
    }
    
    // Getters
    public function get_id() { return $this->id; }
    public function get_title() { return $this->title; }
    public function get_location() { return $this->location; }
    public function get_code() { return $this->code; }
    public function get_created_at() { return $this->created_at; }
    public function get_updated_at() { return $this->updated_at; }
    
    // Setters
    public function set_title($title) { $this->title = sanitize_text_field($title); }
    public function set_location($location) { 
        $this->location = in_array($location, array('header', 'footer', 'body')) ? $location : 'header';
    }
    public function set_code($code) { $this->code = $code; }
    
    // Static methods
    public static function get_all() {
        $snippets_data = PC_HFAP_Database::get_all_snippets();
        $snippets = array();
        
        foreach ($snippets_data as $data) {
            $snippets[] = new self($data);
        }
        
        return $snippets;
    }
    
    public static function get_by_id($id) {
        $data = PC_HFAP_Database::get_snippet($id);
        if ($data) {
            return new self($data);
        }
        return null;
    }
    
    public static function get_headers() {
        $snippets_data = PC_HFAP_Database::get_header_snippets();
        $snippets = array();
        
        foreach ($snippets_data as $data) {
            $snippets[] = new self($data);
        }
        
        return $snippets;
    }
    
    public static function get_footers() {
        $snippets_data = PC_HFAP_Database::get_footer_snippets();
        $snippets = array();
        
        foreach ($snippets_data as $data) {
            $snippets[] = new self($data);
        }
        
        return $snippets;
    }

    public static function get_bodies() {
        $snippets_data = PC_HFAP_Database::get_body_snippets();
        $snippets = array();

        foreach ($snippets_data as $data) {
            $snippets[] = new self($data);
        }

        return $snippets;
    }
}
