<?php

class FieldController {
    private $fieldModel;

    public function __construct() {
        $this->fieldModel = new FieldModel();
    }

    public function store() {
        $data = json_decode(file_get_contents('php://input'), true) ?? $_POST;

        $formId    = (int)($data['form_id'] ?? 0);
        $label     = sanitize($data['label'] ?? '');
        $fieldType = sanitize($data['field_type'] ?? 'text');

        if (!$formId || empty($label)) {
            jsonResponse(['success' => false, 'message' => 'Form ID and label are required.'], 400);
        }

        $validTypes = ['text','email','phone','number','textarea','select','radio','checkbox','date','file','image'];
        if (!in_array($fieldType, $validTypes)) {
            jsonResponse(['success' => false, 'message' => 'Invalid field type.'], 400);
        }

        $id = $this->fieldModel->create([
            'form_id'    => $formId,
            'label'      => $label,
            'field_type' => $fieldType,
            'placeholder'=> sanitize($data['placeholder'] ?? ''),
            'required'   => !empty($data['required']) ? 1 : 0,
            'options'    => sanitize($data['options'] ?? ''),
            'settings'   => json_encode($data['settings'] ?? []),
        ]);

        $field = $this->fieldModel->getById($id);
        jsonResponse(['success' => true, 'field' => $field, 'message' => 'Field added!']);
    }

    public function update($id) {
        $data = json_decode(file_get_contents('php://input'), true) ?? $_POST;
        $field = $this->fieldModel->getById($id);

        if (!$field) {
            jsonResponse(['success' => false, 'message' => 'Field not found.'], 404);
        }

        $this->fieldModel->update($id, [
            'label'      => sanitize($data['label'] ?? $field['label']),
            'field_type' => sanitize($data['field_type'] ?? $field['field_type']),
            'placeholder'=> sanitize($data['placeholder'] ?? ''),
            'required'   => !empty($data['required']) ? 1 : 0,
            'options'    => sanitize($data['options'] ?? ''),
            'settings'   => json_encode($data['settings'] ?? []),
        ]);

        $updated = $this->fieldModel->getById($id);
        jsonResponse(['success' => true, 'field' => $updated, 'message' => 'Field updated!']);
    }

    public function delete($id) {
        $field = $this->fieldModel->getById($id);
        if (!$field) {
            jsonResponse(['success' => false, 'message' => 'Field not found.'], 404);
        }
        $this->fieldModel->delete($id);
        jsonResponse(['success' => true, 'message' => 'Field deleted.']);
    }

    public function reorder() {
        $data = json_decode(file_get_contents('php://input'), true);
        $formId = (int)($data['form_id'] ?? 0);
        $order  = $data['order'] ?? [];

        if (!$formId || empty($order)) {
            jsonResponse(['success' => false, 'message' => 'Invalid data.'], 400);
        }

        $this->fieldModel->reorder($formId, $order);
        jsonResponse(['success' => true, 'message' => 'Order saved.']);
    }

    public function get($id) {
        $field = $this->fieldModel->getById($id);
        if (!$field) {
            jsonResponse(['success' => false, 'message' => 'Field not found.'], 404);
        }
        jsonResponse(['success' => true, 'field' => $field]);
    }
}
