<?php
/**
 * Preset Service Model
 *
 * @package Freelance_Studio
 */

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

class FS_Preset_Service {

    /**
     * Get all preset services
     *
     * @return array
     */
    public static function get_all() {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_preset_services';
        
        // Check if table exists
        if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") !== $table) {
            return array();
        }
        
        return $wpdb->get_results(
            "SELECT * FROM {$table} ORDER BY sort_order ASC, name ASC"
        );
    }

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

    /**
     * Create a preset service
     *
     * @param array $data Service data
     * @return int|false Insert ID or false on failure
     */
    public static function create($data) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_preset_services';
        
        $result = $wpdb->insert(
            $table,
            array(
                'name' => sanitize_text_field($data['name']),
                'description' => isset($data['description']) ? sanitize_textarea_field($data['description']) : '',
                'pricing_type' => isset($data['pricing_type']) ? sanitize_text_field($data['pricing_type']) : 'fixed',
                'default_rate' => isset($data['default_rate']) ? floatval($data['default_rate']) : 0,
                'default_quantity' => isset($data['default_quantity']) ? floatval($data['default_quantity']) : 1,
                'sort_order' => isset($data['sort_order']) ? intval($data['sort_order']) : 0,
            )
        );
        
        if ($result === false) {
            return false;
        }
        
        return $wpdb->insert_id;
    }

    /**
     * Update a preset service
     *
     * @param int $id Service ID
     * @param array $data Service data
     * @return int|false Number of rows updated or false on failure
     */
    public static function update($id, $data) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_preset_services';
        
        return $wpdb->update(
            $table,
            array(
                'name' => sanitize_text_field($data['name']),
                'description' => isset($data['description']) ? sanitize_textarea_field($data['description']) : '',
                'pricing_type' => isset($data['pricing_type']) ? sanitize_text_field($data['pricing_type']) : 'fixed',
                'default_rate' => isset($data['default_rate']) ? floatval($data['default_rate']) : 0,
                'default_quantity' => isset($data['default_quantity']) ? floatval($data['default_quantity']) : 1,
                'sort_order' => isset($data['sort_order']) ? intval($data['sort_order']) : 0,
            ),
            array('id' => $id)
        );
    }

    /**
     * Delete a preset service
     *
     * @param int $id Service ID
     * @return int|false Number of rows deleted or false on failure
     */
    public static function delete($id) {
        global $wpdb;
        
        $table = $wpdb->prefix . 'fs_preset_services';
        
        return $wpdb->delete($table, array('id' => $id), array('%d'));
    }
}
