<?php
namespace App\Modules\Spiritual\Controllers;

class SpiritualController extends \Core\Controller {

    public function index() {
        $model = $this->model('Spiritual/Models/SpiritualModel');
        $date = isset($_GET['date']) ? sanitize_input($_GET['date']) : date('Y-m-d');
        
        $data = $model->getDailyData($this->userId, $date);

        $this->view('Spiritual/Views/dashboard', [
            'title'       => 'الروحانيات - ' . $date,
            'date'        => $date,
            'log'         => $data['log'],
            'prayers'     => $data['prayers'],
            'adhkar'      => $data['adhkar'],
            'quran'       => $data['quran'],
            'charity'     => $data['charity'],
            'streaks'     => $data['streaks'],
            'records'     => $data['records'],
            'progress'    => $data['progress'],
            'status'      => $data['status'],
            'is_locked'   => $data['is_locked'],
            'feedback'    => $data['feedback'],
            'consistency' => $data['consistency'] ?? 50,
            'state'       => $data['state'] ?? 'medium'
        ]);
    }

    public function archive() {
        $model = $this->model('Spiritual/Models/SpiritualModel');
        $this->view('Spiritual/Views/archive', [
            'title' => 'سجل الروحانيات والأداء',
            'days' => $model->getArchive($this->userId),
            'summary' => $model->getWeeklySummary($this->userId)
        ]);
    }

    public function toggle() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $input = json_decode(file_get_contents('php://input'), true);
            $model = $this->model('Spiritual/Models/SpiritualModel');
            
            $date = $input['date'] ?? date('Y-m-d');
            $table = $input['table'] ?? '';
            $field = $input['field'] ?? '';
            
            if ($table && $field) {
                $model->getDailyData($this->userId, $date);
                $model->toggleStatus($this->userId, $date, $table, $field, null);
                $newData = $model->getDailyData($this->userId, $date);
                
                echo json_encode(['success' => true, 'progress' => $newData['progress'], 'streaks' => $newData['streaks'], 'status' => $newData['status'], 'consistency' => $newData['consistency'], 'state' => $newData['state']]);
                exit;
            }
        }
        echo json_encode(['success' => false]);
    }

    public function updateText() {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $input = json_decode(file_get_contents('php://input'), true);
            $model = $this->model('Spiritual/Models/SpiritualModel');
            
            $date = $input['date'] ?? date('Y-m-d');
            
            $table = 'spiritual_daily_logs';
            $field = $input['field'] ?? '';
            $value = $input['value'] ?? '';
            
            if (in_array($field, ['heart_state', 'daily_rating', 'daily_goal'])) {
                $model->getDailyData($this->userId, $date);
                $model->toggleStatus($this->userId, $date, $table, $field, $value);
                echo json_encode(['success' => true]);
                exit;
            }
        }
        echo json_encode(['success' => false]);
    }

    public function deleteDay($date) {
        if ($date) {
            $model = $this->model('Spiritual/Models/SpiritualModel');
            $model->deleteRecord($this->userId, sanitize_input($date));
            set_flash_message('spiritual', 'تم مسح سجل اليوم بنجاح.', 'success');
        }
        redirect('/spiritual/archive');
    }
}
