<?php

class FreezeController extends Controller
{
    public function index(Request $request): void
    {
        $db = $this->db();
        $totalPending = (int) $db->fetchColumn("SELECT COUNT(*) FROM member_freezes WHERE status = 'pending'");
        $totalActive  = (int) $db->fetchColumn("SELECT COUNT(*) FROM member_freezes WHERE status = 'active'");
        $totalApproved = (int) $db->fetchColumn("SELECT COUNT(*) FROM member_freezes WHERE status = 'approved'");
        $totalEnded   = (int) $db->fetchColumn("SELECT COUNT(*) FROM member_freezes WHERE status = 'ended'");

        $this->view('freezes/index', [
            'title' => lang('subs.freezes'),
            'breadcrumbs' => [['label' => lang('subs.freezes')]],
            'totalPending'  => $totalPending,
            'totalActive'   => $totalActive,
            'totalApproved' => $totalApproved,
            'totalEnded'    => $totalEnded,
        ]);
    }

    public function create(Request $request): void
    {
        $memberId = $request->get('member_id');
        $member = null;
        if ($memberId) {
            $member = $this->db()->fetchOne(
                "SELECT m.id, m.membership_number, m.full_name_en, m.full_name_ar, m.status, m.membership_type_id, mt.name_en AS type_name
                 FROM members m JOIN membership_types mt ON m.membership_type_id = mt.id
                 WHERE m.id = ? AND m.is_deleted = 0",
                [$memberId]
            );
        }

        $this->view('freezes/create', [
            'title' => lang('subs.create_freeze'),
            'breadcrumbs' => [
                ['label' => lang('subs.freezes'), 'url' => '/freezes'],
                ['label' => lang('subs.create_freeze')],
            ],
            'member' => $member,
        ]);
    }

    public function show(Request $request): void
    {
        $id = (int) $request->routeParam('id');
        $db = $this->db();

        $freeze = $db->fetchOne(
            "SELECT mf.*, m.membership_number, m.full_name_en AS member_name, m.primary_phone,
                    fp.name_en AS policy_name, fp.policy_code,
                    u1.full_name_en AS approved_by_name, u2.full_name_en AS ended_by_name, u3.full_name_en AS created_by_name
             FROM member_freezes mf
             JOIN members m ON mf.member_id = m.id
             LEFT JOIN freeze_policies fp ON mf.policy_id = fp.id
             LEFT JOIN auth_users u1 ON mf.approved_by = u1.id
             LEFT JOIN auth_users u2 ON mf.ended_by = u2.id
             LEFT JOIN auth_users u3 ON mf.created_by = u3.id
             WHERE mf.id = ?",
            [$id]
        );

        if (!$freeze) { $this->abort(404); return; }

        $extensions = $db->fetchAll(
            "SELECT fe.*, u.full_name_en AS approved_by_name
             FROM freeze_extensions fe LEFT JOIN auth_users u ON fe.approved_by = u.id
             WHERE fe.freeze_id = ? ORDER BY fe.created_at DESC",
            [$id]
        );

        $this->view('freezes/show', [
            'title' => lang('subs.freeze_details') . ' #' . $freeze['freeze_number'],
            'breadcrumbs' => [
                ['label' => lang('subs.freezes'), 'url' => '/freezes'],
                ['label' => $freeze['freeze_number']],
            ],
            'freeze'     => $freeze,
            'extensions' => $extensions,
        ]);
    }

    public function policies(Request $request): void
    {
        $types = $this->db()->fetchAll("SELECT id, name_en, name_ar FROM membership_types WHERE is_active = 1 ORDER BY sort_order");

        $this->view('freezes/policies', [
            'title' => lang('subs.freeze_policies'),
            'breadcrumbs' => [
                ['label' => lang('subs.freezes'), 'url' => '/freezes'],
                ['label' => lang('subs.freeze_policies')],
            ],
            'types' => $types,
        ]);
    }
}