<?php

defined('BASEPATH') or exit('No direct script access allowed');

class PropertyMaintenance_model extends App_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    public function create($data)
    {
        $data = hooks()->apply_filters('propertybase_before_property_maintenance_added', $data);

        $this->db->insert(db_prefix() . 'propertybase_properties_maintenance', $data);
        $insert_id = $this->db->insert_id();

        if ($insert_id) {
            hooks()->do_action('propertybase_after_property_maintenance_added', $insert_id);

            $attachments = propertybase_handle_maintenance_request_attachments($insert_id);
            if ($attachments) {
                $this->insertRequestFilesToDatabase($attachments, $insert_id);
            }

            return $insert_id;
        }

        return false;
    }

    public function get($id)
    {
        $this->db->where('id', $id);
        $property_maintenance = $this->db->get(db_prefix() . 'propertybase_properties_maintenance')->row();

        if ($property_maintenance) {
            $property_maintenance->attachments = $this->getRequestAttachments($property_maintenance->id);
        }

        return hooks()->apply_filters('propertybase_get_property_maintenance', $property_maintenance);
    }

    public function get_all()
    {
        $property_maintenances = $this->db->get(db_prefix() . 'propertybase_properties_maintenance')->result_array();
        return hooks()->apply_filters('propertybase_get_property_maintenances', $property_maintenances);
    }

    public function update($id, $data)
    {
        $data = hooks()->apply_filters('propertybase_before_property_maintenance_updated', $data, $id);

        $this->db->where('id', $id);
        $this->db->update(db_prefix() . 'propertybase_properties_maintenance', $data);

        if ($this->db->affected_rows() > 0) {
            hooks()->do_action('propertybase_after_property_maintenance_updated', $id);
            return true;
        }

        return false;
    }

    public function delete($id)
    {
        hooks()->do_action('propertybase_before_property_maintenance_deleted', $id);

        $this->db->where('id', $id);
        $this->db->delete(db_prefix() . 'propertybase_properties_maintenance');

        $directory = PROPERTYBASE_PROPERTY_MAINTENANCE_REQUESTS_PATH . $id . '/';

        if (is_dir($directory)) {
            delete_dir($directory);
        }

        $this->db->where('request_id', $id);
        $this->db->delete(db_prefix() . 'propertybase_maintenance_request_files');

        if ($this->db->affected_rows() > 0) {
            hooks()->do_action('propertybase_after_property_maintenance_deleted', $id);
            return true;
        }

        return false;
    }

    public function get_client_property_maintenance_requests($property_id)
    {
        $this->db->where([
            'property_id' => $property_id,
            'requested_by_type' => 'customer',
            'user_id' => get_client_user_id()
        ]);
        $properties = $this->db->get(db_prefix() . 'propertybase_properties_maintenance')->result_array();

        foreach ($properties as &$property) {
            $property['attachments'] = $this->getRequestAttachments($property['id']);
        }

        return hooks()->apply_filters('propertybase_get_property', $properties);
    }

    public function getRequestAttachments($id)
    {
        $this->db->where('request_id', $id);
        return $this->db->get(db_prefix() . 'propertybase_maintenance_request_files')->result_array();
    }

    public function insertRequestFilesToDatabase($attachments, $requestId)
    {
        foreach ($attachments as $attachment) {
            $attachment['request_id'] = $requestId;
            $attachment['created_at'] = date('Y-m-d H:i:s');
            $attachment['filename'] = $attachment['file_name'];

            unset($attachment['file_name'], $attachment['filetype']);

            $this->db->insert(db_prefix() . 'propertybase_maintenance_request_files', $attachment);
        }
    }

    public function _search_maintenance_requests(array $data, $limit = 0)
    {
        $q = $data['q'];
        $customer_id = $data['customer_id'];

        $this->db->select();
        $this->db->from(db_prefix() . 'propertybase_properties_maintenance');

        if (!empty($customer_id) && is_numeric($customer_id)) {
            $this->db->where(db_prefix() . 'propertybase_properties_maintenance.requested_by_type', 'customer');
            $this->db->where(db_prefix() . 'propertybase_properties_maintenance.user_id', $customer_id);
        }

        $this->db->where('(maintenance_title LIKE "%' . $this->db->escape_like_str($q) . '%" ESCAPE \'!\')');

        if ($limit != 0) {
            $this->db->limit($limit);
        }
        $this->db->order_by('maintenance_title', 'ASC');

        return $this->db->get()->result_array();
    }

}