<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Domain;
use App\Models\DomainLogin;
use Illuminate\Support\Facades\Auth;

class DomainController extends Controller
{
    public function index()
    {
        $domains = Domain::with(['user', 'logins'])->orderBy('created_at', 'desc')->get();
        return view('admin.domain.domain', compact('domains'));
    }

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

        Domain::create([
            'user_id' => Auth::id(),
            'name' => $request->name,
            'url' => $request->url,
        ]);

        return redirect()->back()->with('success', 'Website added successfully.');
    }

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

        $domain = Domain::findOrFail($id);
        $domain->update([
            'name' => $request->name,
            'url' => $request->url,
        ]);

        return redirect()->back()->with('success', 'Website updated successfully.');
    }

    public function destroy($id)
    {
        $domain = Domain::findOrFail($id);
        $domain->delete();
        return redirect()->back()->with('success', 'Website deleted successfully.');
    }

    public function storeLogin(Request $request)
    {
        $request->validate([
            'domain_id' => 'required|exists:domains,id',
            'login_type' => 'required|string|max:255',
            'username' => 'nullable|string|max:255',
            'password' => 'nullable|string',
            'login_url' => 'nullable|string|max:255',
        ]);

        DomainLogin::create($request->all());

        return redirect()->back()->with('success', 'Login credential added successfully.');
    }

    public function updateLogin(Request $request, $id)
    {
        $request->validate([
            'login_type' => 'required|string|max:255',
            'username' => 'nullable|string|max:255',
            'password' => 'nullable|string',
            'login_url' => 'nullable|string|max:255',
        ]);

        $login = DomainLogin::findOrFail($id);
        
        $updateData = [
            'login_type' => $request->login_type,
            'username' => $request->username,
            'login_url' => $request->login_url,
        ];
        
        if ($request->filled('password')) {
            $updateData['password'] = $request->password;
        }

        $login->update($updateData);

        return redirect()->back()->with('success', 'Login credential updated successfully.');
    }

    public function destroyLogin($id)
    {
        $login = DomainLogin::findOrFail($id);
        $login->delete();
        return redirect()->back()->with('success', 'Login credential deleted successfully.');
    }
}
