<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\TeacherProfile;
use App\Models\Subject;
use App\Models\Level;

class TeacherController extends Controller
{
	
    public function index()
    {
		$teachers = \App\Models\TeacherProfile::with('user')->get();

		return view('admin.teachers.index', compact('teachers'));
    }
	
    public function create()
    {
        $subjects = Subject::orderBy('name')->get();
        $levels   = Level::orderBy('name')->get();

        return view('admin.teachers.create', compact('subjects', 'levels'));
    }
	
    public function store(Request $request)
    {
        // Later we’ll add validation here

        // Create the User
        $user = User::create([
            'name'     => $request->name,
            'email'    => $request->email,
            'password' => Hash::make($request->password),
        ]);

		$user->assignRole('teacher');

        // Handle profile picture upload (placeholder for now)
        $pathToFile = null;
        if ($request->hasFile('picture')) {
            $pathToFile = $request->file('picture')->store('teacher_pictures', 'public');
        }

        // Create the TeacherProfile
        $teacher = TeacherProfile::create([
            'user_id'    => $user->id,
            'bio'        => $request->bio,
            'location'   => $request->location,
            'hourly_rate'=> $request->hourly_rate,
            'picture'    => $pathToFile,
        ]);

        // For later: attach subjects and levels
        // $teacher->subjects()->sync($request->subjects);
        // $teacher->levels()->sync($request->levels);

        // Redirect somewhere (dashboard for now)
        return redirect()->route('admin.dashboard')
            ->with('success', 'Teacher added successfully!');
    }
	
	
}
