<?php
/**
 * Proposal Section Template Model
 *
 * @package Freelance_Studio
 */

class FS_Proposal_Section {

    /**
     * Position constants
     */
    const POSITION_BEFORE = 'before_pricing';
    const POSITION_AFTER = 'after_pricing';

    /**
     * Get all section templates
     *
     * @param string|null $position Filter by position
     * @return array
     */
    public static function get_all($position = null) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_proposal_section_templates';
        
        $sql = "SELECT * FROM {$table}";
        
        if ($position) {
            $sql .= $wpdb->prepare(" WHERE position = %s", $position);
        }
        
        $sql .= " ORDER BY sort_order ASC, title ASC";
        
        return $wpdb->get_results($sql); // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared
    }

    /**
     * Get a section template by ID
     *
     * @param int $id Section ID
     * @return object|null
     */
    public static function get($id) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_proposal_section_templates';
        
        return $wpdb->get_row($wpdb->prepare(
            "SELECT * FROM {$table} WHERE id = %d",
            $id
        ));
    }

    /**
     * Create a new section template
     *
     * @param array $data Section data
     * @return int|false Insert ID or false on failure
     */
    public static function create($data) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_proposal_section_templates';
        
        // Get max sort order
        $max_order = $wpdb->get_var("SELECT MAX(sort_order) FROM {$table}");
        $sort_order = $max_order ? $max_order + 1 : 0;
        
        $result = $wpdb->insert(
            $table,
            array(
                'title' => sanitize_text_field($data['title']),
                'content' => wp_kses_post($data['content']),
                'position' => isset($data['position']) ? sanitize_text_field($data['position']) : self::POSITION_BEFORE,
                'sort_order' => $sort_order,
            ),
            array('%s', '%s', '%s', '%d')
        );
        
        if ($result === false) {
            error_log('FS_Proposal_Section::create failed: ' . $wpdb->last_error);
            return false;
        }
        
        return $wpdb->insert_id;
    }

    /**
     * Update a section template
     *
     * @param int $id Section ID
     * @param array $data Section data
     * @return bool
     */
    public static function update($id, $data) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_proposal_section_templates';
        
        $update_data = array();
        $format = array();
        
        if (isset($data['title'])) {
            $update_data['title'] = sanitize_text_field($data['title']);
            $format[] = '%s';
        }
        
        if (isset($data['content'])) {
            $update_data['content'] = wp_kses_post($data['content']);
            $format[] = '%s';
        }
        
        if (isset($data['position'])) {
            $update_data['position'] = sanitize_text_field($data['position']);
            $format[] = '%s';
        }
        
        if (isset($data['sort_order'])) {
            $update_data['sort_order'] = intval($data['sort_order']);
            $format[] = '%d';
        }
        
        if (empty($update_data)) {
            return false;
        }
        
        return $wpdb->update($table, $update_data, array('id' => $id), $format, array('%d')) !== false;
    }

    /**
     * Delete a section template
     *
     * @param int $id Section ID
     * @return bool
     */
    public static function delete($id) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_proposal_section_templates';
        
        return $wpdb->delete($table, array('id' => $id), array('%d')) !== false;
    }

    /**
     * Get position labels
     *
     * @return array
     */
    public static function get_position_labels() {
        return array(
            self::POSITION_BEFORE => __('Before Pricing', 'freelance-studio'),
            self::POSITION_AFTER => __('After Pricing', 'freelance-studio'),
        );
    }
}
