<?php

namespace App\Controllers;

use App\Controllers\Base\SettingController as BaseController;

use App\Resources\SettingResource;

class SettingController extends BaseController
{
    /**
     * Override methods here to add custom logic.
     */

    /**
     * Get active settings with pagination
     * GET /settings
     */
    public function index()
    {
        // Fetch only the first active setting using the query builder ->one()
        $setting = $this->model->findBuilder()->where(['status' => 1])->one();

        if (!$setting) {
            // Fallback: if no active setting, send empty or handle accordingly
            return $this->json([
                'success' => true,
                'message' => 'No active setting found',
                'item' => null
            ]);
        }

        return SettingResource::make($setting);
    }

    public function store()
    {
        $this->requireRole('superadmin');

        $validated = $this->validate([
            'setting' => 'string'
        ]);

        // Force status to 1 for new settings so they are visible
        $validated['status'] = 1;

        try {
            $id = $this->model->create($validated);
            $setting = $this->model->find($id);
            return $this->created(SettingResource::make($setting));
        } catch (\PDOException $e) {
            $this->databaseError('Failed to create setting', $e);
        }
    }

    public function update()
    {
        $this->requireRole('superadmin');

        $id = $this->request->param('id');
        $setting = $this->model->find($id);

        if (!$setting) {
            throw new \Exception('Setting not found', 404);
        }

        $validated = $this->validate([
            'setting' => 'string'
        ]);

        try {
            $this->model->update($id, $validated);
            return SettingResource::make($this->model->find($id));
        } catch (\PDOException $e) {
            $this->databaseError('Failed to update setting', $e);
        }
    }

    public function destroy()
    {
        $this->requireRole('superadmin');
        return parent::destroy();
    }

    /**
     * Regenerate Daily Attendance QR Token
     * POST /setting/regenerate-daily-qr
     */
    public function regenerateDailyQrToken()
    {
        $this->requireRole('superadmin');

        $setting = $this->model->findBuilder()->where(['status' => 1])->one();
        if (!$setting) {
            return $this->json(['success' => false, 'message' => 'No active setting found'], 404);
        }

        $config = json_decode($setting['setting'], true) ?: [];
        $config['daily_attendance_qr_token'] = bin2hex(random_bytes(16)) . '_' . time();
        
        $this->model->update($setting['id'], [
            'setting' => json_encode($config)
        ]);

        return $this->json([
            'success' => true,
            'message' => 'Daily QR Token regenerated',
            'qr_token' => $config['daily_attendance_qr_token']
        ]);
    }
}
