<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SiteSlideModel;

class SiteSlideController extends BaseController
{
    public function index()
    {
        $slides = (new SiteSlideModel())->forCompany($this->currentCompanyId())->orderBy('sort_order', 'ASC')->findAll();
        return view('admin/site_slides/index', ['slides' => $slides]);
    }

    public function create()
    {
        return view('admin/site_slides/form', ['slide' => null]);
    }

    public function store()
    {
        $companyId = $this->currentCompanyId();
        $imageFile = $this->request->getFile('image');

        if (! $imageFile || ! $imageFile->isValid()) {
            return redirect()->back()->withInput()->with('error', 'يجب رفع صورة للسلايد');
        }

        if (! is_dir(FCPATH . 'uploads/slides')) {
            mkdir(FCPATH . 'uploads/slides', 0755, true);
        }
        $newName = 'slide_' . $companyId . '_' . time() . '_' . rand(100, 999) . '.' . $imageFile->getExtension();
        $imageFile->move(FCPATH . 'uploads/slides', $newName);

        (new SiteSlideModel())->insert([
            'company_id' => $companyId,
            'image' => $newName,
            'title' => $this->request->getPost('title'),
            'subtitle' => $this->request->getPost('subtitle'),
            'sort_order' => $this->request->getPost('sort_order') ?: 0,
            'is_active' => $this->request->getPost('is_active') ? 1 : 0,
        ]);

        return redirect()->to('/admin/site-slides')->with('message', lang('App.saved_successfully'));
    }

    public function edit($id)
    {
        $slide = (new SiteSlideModel())->forCompany($this->currentCompanyId())->find($id);
        if (! $slide) return redirect()->to('/admin/site-slides');
        return view('admin/site_slides/form', ['slide' => $slide]);
    }

    public function update($id)
    {
        $data = [
            'title' => $this->request->getPost('title'),
            'subtitle' => $this->request->getPost('subtitle'),
            'sort_order' => $this->request->getPost('sort_order') ?: 0,
            'is_active' => $this->request->getPost('is_active') ? 1 : 0,
        ];

        $imageFile = $this->request->getFile('image');
        if ($imageFile && $imageFile->isValid() && ! $imageFile->hasMoved()) {
            if (! is_dir(FCPATH . 'uploads/slides')) {
                mkdir(FCPATH . 'uploads/slides', 0755, true);
            }
            $newName = 'slide_' . $this->currentCompanyId() . '_' . time() . '.' . $imageFile->getExtension();
            $imageFile->move(FCPATH . 'uploads/slides', $newName);
            $data['image'] = $newName;
        }

        (new SiteSlideModel())->update($id, $data);
        return redirect()->to('/admin/site-slides')->with('message', lang('App.saved_successfully'));
    }

    public function delete($id)
    {
        (new SiteSlideModel())->delete($id);
        return redirect()->to('/admin/site-slides')->with('message', lang('App.deleted_successfully'));
    }
}
