<?php

namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;
use App\Models\MailTemplateModel;

class EmailTemplateController extends ResourceController
{   
    protected $format    = 'json';
    protected $myLogger;
    protected $mailTemplateModel;


    public function __construct()
    {
        $this->mailTemplateModel = new MailTemplateModel();
    }

    public function index()
    {   
        $org_id = $this->request->getGet('org_id');
    
        if (!$org_id) {
            log_message('error', 'Organization ID missing in index() template list');
            return $this->failNotFound('Organization ID is required');
        }
        
         //fetch existing template data for the give ORG ID
        $mailTemplateData = $this->mailTemplateModel->where('org_id', $org_id)->where('template_name !=', 'forex')->findAll();
    
        if (!$mailTemplateData) {
            log_message('error', "No mail template found for org_id: {$org_id} in index()");
            return $this->failNotFound('No template found');
        }
    
        log_message('error', "Mail templates found for org_id: {$org_id} in index()");
        return $this->respond([
            'status' => 200,
            'message' => 'Template found',
            'data' => $mailTemplateData
        ]);
    }

    public function getForexTemplate()
    {   
        $template_name = $this->request->getGet('template_name');
    
        if (!$template_name) {
            log_message('error', 'template_name missing ');
            return $this->failNotFound('template_name is required');
        }
        
         //fetch existing template data for the give ID
         $singleMailTemplateData = $this->mailTemplateModel->where('template_name', $template_name)->first();
    
         if (!$singleMailTemplateData) {
             log_message('error', "Template not found for template: {$template_name} in find()");
             return $this->failNotFound('Template not found');
         }
     
         log_message('error', "Template found for template: {$template_name} in find()");
         return $this->respond([
             'status' => 200,
             'message' => 'Template found',
             'data' => $singleMailTemplateData
         ]);
    }
    
    public function find($id = null)
    {
        if (!$id) {
            log_message('error', 'Template ID missing in find()');
            return $this->failNotFound('Template id is required');
        }
    
         //fetch existing template data for the give ID
        $singleMailTemplateData = $this->mailTemplateModel->find($id);
    
        if (!$singleMailTemplateData) {
            log_message('error', "Template not found for id: {$id} in find()");
            return $this->failNotFound('Template not found');
        }
    
        log_message('error', "Template found for id: {$id} in find()");
        return $this->respond([
            'status' => 200,
            'message' => 'Template found',
            'data' => $singleMailTemplateData
        ]);
    }
    
    public function update($id = null)
    {
        if (!$id) {
            log_message('error', 'Template ID missing in update()');
            return $this->failNotFound('Template ID is required');
        }
    
        //request data
        $data = $this->request->getJSON(true);
        $req['body_html'] = (string)$data['body_html'];
        $req['subject'] = $data['subject'];
        // print_r($data); die; 
    
        if (empty($data)) {
            log_message('error', "No data provided for update in update() with id: {$id}");
            return $this->failValidationErrors('No data provided for update');
        }
    
        //fetch existing template data for the give ID
        $singleMailTemplateData = $this->mailTemplateModel->find($id);
        if (!$singleMailTemplateData) {
            log_message('error', "Template not found for update() with id: {$id}");
            return $this->failNotFound('Template not found');
        }
    
        try {
            $this->mailTemplateModel->update($id, $req);
            log_message('error', "Template updated successfully for id: {$id} in update()");
            return $this->respond([
                'status'  => 200,
                'message' => 'Template updated successfully',
                'data'    => $this->mailTemplateModel->find($id)
            ]);
        } catch (\Exception $e) {
            log_message('error', "Failed to update template in update() with id: {$id}, Error: {$e->getMessage()}");
            return $this->failServerError('Failed to update template: ' . $e->getMessage());
        }
    }
    
}
