<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\FtpServer;
use Illuminate\Http\Request;

class FtpServerController extends Controller
{
    public function index()
    {
        $ftpServers = FtpServer::select('id', 'name', 'link')->get();
        return sendSuccessResponse($ftpServers->map(function ($ftpServer) {
            return [
                'id' => $ftpServer->id,
                'name' => $ftpServer->name,
                'link' => $ftpServer->link,
            ];
        }));
    }

    public function ftpServers()
    {
        return view('Backend.FtpServers.ftp-servers', [
            'ftpServers' => FtpServer::select('id', 'name', 'link')->get()
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'link' => 'required|string|max:255|url',
        ]);

        FtpServer::create($request->all());
        return back()
            ->with('success', 'FTP server added successfully')
            ->with('toast', true);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'link' => 'required|string|max:255|url',
        ]);

        FtpServer::find($id)->update($request->all());
        return back()
            ->with('success', 'FTP server updated successfully')
            ->with('toast', true);
    }

    public function delete(Request $request)
    {
        FtpServer::find($request->ftp_server_id)->delete();
        return back()
            ->with('success', 'FTP server deleted successfully')
            ->with('toast', true);
    }
}
