<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Helpers\Auth;
use App\Models\CustomLink;
use App\Models\AuditLog;

class LinkController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        Auth::requireLogin();
    }

    public function index()
    {
        $model = new CustomLink();
        $links = $model->all();
        $categories = $model->getCategories();
        $this->view('links/index', ['links' => $links, 'categories' => $categories]);
    }

    public function create()
    {
        $this->view('links/form');
    }

    public function store()
    {
        $model = new CustomLink();
        $model->create([
            'title' => $_POST['title'] ?? '',
            'url' => $_POST['url'] ?? '',
            'description' => $_POST['description'] ?? '',
            'icon' => $_POST['icon'] ?? 'fas fa-link',
            'target' => $_POST['target'] ?? '_blank',
            'category' => $_POST['category'] ?? null,
            'is_active' => isset($_POST['is_active']) ? 1 : 0,
            'created_by' => Auth::id(),
        ]);

        $audit = new AuditLog();
        $audit->log(Auth::id(), 'link.create', 'custom_links', $model->db->lastInsertId(), "Created link: {$_POST['title']}");

        $_SESSION['success'] = 'Link created successfully';
        $this->redirect('/links');
    }

    public function edit($params)
    {
        $model = new CustomLink();
        $link = $model->find($params['id'] ?? null);
        if (!$link) {
            $_SESSION['error'] = 'Link not found';
            $this->redirect('/links');
        }
        $this->view('links/form', ['link' => $link]);
    }

    public function update($params)
    {
        $id = $params['id'] ?? null;
        $model = new CustomLink();
        $link = $model->find($id);
        if (!$link) {
            $_SESSION['error'] = 'Link not found';
            $this->redirect('/links');
        }

        $model->update($id, [
            'title' => $_POST['title'] ?? '',
            'url' => $_POST['url'] ?? '',
            'description' => $_POST['description'] ?? '',
            'icon' => $_POST['icon'] ?? 'fas fa-link',
            'target' => $_POST['target'] ?? '_blank',
            'category' => $_POST['category'] ?? null,
            'is_active' => isset($_POST['is_active']) ? 1 : 0,
        ]);

        $audit = new AuditLog();
        $audit->log(Auth::id(), 'link.update', 'custom_links', $id, "Updated link: {$_POST['title']}");

        $_SESSION['success'] = 'Link updated successfully';
        $this->redirect('/links');
    }

    public function delete($params)
    {
        $id = $params['id'] ?? null;
        $model = new CustomLink();
        $link = $model->find($id);
        if ($link) {
            $audit = new AuditLog();
            $audit->log(Auth::id(), 'link.delete', 'custom_links', $id, "Deleted link: {$link->title}");
            $model->delete($id);
            $_SESSION['success'] = 'Link deleted';
        }
        $this->redirect('/links');
    }
}
