<?php
class CardTypeController extends Controller
{
    public function index(Request $request): void
    {
        $db = $this->db();
        $types = $db->fetchAll("SELECT * FROM card_types ORDER BY name_en");

        $this->view('cards/types', [
            'title'       => lang('cards.card_types'),
            'breadcrumbs' => [
                ['label' => lang('cards.module'), 'url' => '/cards'],
                ['label' => lang('cards.card_types')],
            ],
            'types' => $types,
        ]);
    }

    public function store(Request $request): void
    {
        if (!CSRF::validate($request->csrfToken())) {
            $this->json(['success' => false, 'message' => 'Invalid CSRF token'], 403);
            return;
        }

        $validator = new Validator($request->all(), [
            'type_code'       => 'required|string|max:20',
            'name_en'         => 'required|string|max:100',
            'name_ar'         => 'required|string|max:100',
            'applicable_to'   => 'required|in:member,dependent,guest,staff',
            'validity_months' => 'nullable|integer|min:1',
            'fee'             => 'required|decimal|min:0',
            'replacement_fee' => 'required|decimal|min:0',
            'has_photo'       => 'nullable|in:0,1',
            'has_barcode'     => 'nullable|in:0,1',
            'has_qr_code'     => 'nullable|in:0,1',
        ]);

        if ($validator->fails()) {
            $this->json(['success' => false, 'errors' => $validator->errors()], 422);
            return;
        }

        $data = $validator->validated();
        $data['has_photo']   = $data['has_photo'] ?? 1;
        $data['has_barcode'] = $data['has_barcode'] ?? 1;
        $data['has_qr_code'] = $data['has_qr_code'] ?? 0;
        $data['is_active']   = 1;

        $db = $this->db();
        $id = $db->insert('card_types', $data);

        $this->audit('create', 'card_types', $id, "Card type created: {$data['name_en']}");
        $this->json(['success' => true, 'message' => lang('common.save') . ' ✓']);
    }

    public function update(Request $request): void
    {
        if (!CSRF::validate($request->csrfToken())) {
            $this->json(['success' => false, 'message' => 'Invalid CSRF token'], 403);
            return;
        }

        $id = (int)$request->routeParam('id');

        $validator = new Validator($request->all(), [
            'name_en'         => 'required|string|max:100',
            'name_ar'         => 'required|string|max:100',
            'applicable_to'   => 'required|in:member,dependent,guest,staff',
            'validity_months' => 'nullable|integer|min:1',
            'fee'             => 'required|decimal|min:0',
            'replacement_fee' => 'required|decimal|min:0',
            'has_photo'       => 'nullable|in:0,1',
            'has_barcode'     => 'nullable|in:0,1',
            'has_qr_code'     => 'nullable|in:0,1',
            'is_active'       => 'nullable|in:0,1',
        ]);

        if ($validator->fails()) {
            $this->json(['success' => false, 'errors' => $validator->errors()], 422);
            return;
        }

        $data = $validator->validated();
        $data['has_photo']   = $data['has_photo'] ?? 1;
        $data['has_barcode'] = $data['has_barcode'] ?? 1;
        $data['has_qr_code'] = $data['has_qr_code'] ?? 0;
        $data['is_active']   = $data['is_active'] ?? 1;

        $db = $this->db();
        $old = $db->fetchOne("SELECT * FROM card_types WHERE id = ?", [$id]);
        $db->update('card_types', $data, 'id = ?', [$id]);

        $this->audit('update', 'card_types', $id, "Card type updated", $old, $data);
        $this->json(['success' => true, 'message' => lang('common.save') . ' ✓']);
    }
}