<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\BackendController;
use App\Http\Controllers\Admin\CategoryController;
use App\Http\Controllers\Admin\TagController;
use App\Http\Controllers\Admin\BlogPostController;
use App\Http\Controllers\Admin\PageController;
use App\Http\Controllers\SubscriberController;
use App\Http\Controllers\Admin\SiteSettingController;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Admin\SocialMediaLinkController;

/*
|--------------------------------------------------------------------------
| Frontend Routes
|--------------------------------------------------------------------------
*/

Route::get('/', [HomeController::class, 'Index'])->name('home');

Route::view('/contact-us', 'frontend.otherpages.contact')->name('contact');
Route::view('/about-us', 'frontend.otherpages.about')->name('about');

Route::get('/category/{slugurl}', [HomeController::class, 'Category'])->name('category');
Route::get('/tag/{slugurl}', [HomeController::class, 'Tag'])->name('tag');
Route::get('/search', [HomeController::class, 'Search'])->name('search');
Route::get('/archive/{year}/{month}', [HomeController::class, 'Archive'])->name('archive');

Route::post('/comments', [HomeController::class, 'Comment'])->name('post.comments');
Route::post('/comment-store', [HomeController::class, 'Comment'])->name('comment.save');

Route::post('/subscriber-store', [SubscriberController::class, 'subscriber'])->name('newsletter');
Route::get('/unsubscribe/{token}', [SubscriberController::class, 'unsubscribe'])->name('unsubscribe');

Route::prefix('/page')->group(function () {
    Route::get('/contact', [HomeController::class, 'Contact'])->name('contact.page');
    Route::get('/about', [HomeController::class, 'about'])->name('about.page');
    Route::get('/privacy-policy', [HomeController::class, 'PrivacyPolicy'])->name('privacy.policy');
    Route::get('/terms-and-conditions', [HomeController::class, 'TermsAndConditions'])->name('terms.and.conditions');
});

/*
|--------------------------------------------------------------------------
| Auth Routes
|--------------------------------------------------------------------------
*/

Route::get('/admin', [UserController::class, 'LogIn'])->name('login');
Route::post('/login-data', [UserController::class, 'Match'])->name('login.data');
Route::post('/register-data', [UserController::class, 'Register'])->name('register.data');
Route::get('/sign/up-form', [UserController::class, 'SignUp'])->name('signup');
Route::get('/forget-password', [UserController::class, 'ForgetPassword'])->name('forget.pass');
Route::get('/logout', [UserController::class, 'logout'])->name('logout');

Route::get('user/userprofile', [UserController::class, 'UserProfile'])->name('userprofile');
Route::get('user/userprofile/{slug}', [HomeController::class, 'AuthorProfile'])->name('author.profile');


/*
|--------------------------------------------------------------------------
| Admin Dashboard (Only Admin)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:admin'])->prefix('admin')->group(function () {



    // Users
    Route::get('user-list', [BackendController::class, 'List'])->name('user.list');
    Route::get('user-add', [BackendController::class, 'Add'])->name('user.add');
    Route::post('user-store', [BackendController::class, 'Store'])->name('user.store');
    Route::get('user-edit/{id}', [BackendController::class, 'Edit'])->name('edit.user');
    Route::post('user-update/{id}', [BackendController::class, 'Update'])->name('update.user');
    Route::get('user-status/{id}', [BackendController::class, 'Status'])->name('user.status');
    Route::delete('user-delete/{id}', [BackendController::class, 'Delete'])->name('delete.user');

    // Social Media
    Route::resource('social-media', SocialMediaLinkController::class);
    Route::post('social-media/sort', [SocialMediaLinkController::class, 'sort'])->name('social-media.sort');

    // Categories
    Route::get('category-list', [CategoryController::class, 'List'])->name('category.list');
    Route::get('category-add', [CategoryController::class, 'Add'])->name('category.add');
    Route::post('category-store', [CategoryController::class, 'Store'])->name('category.store');
    Route::get('category-edit/{id}', [CategoryController::class, 'Edit'])->name('edit.category');
    Route::put('category-update/{id}', [CategoryController::class, 'Update'])->name('update.category');
    Route::get('category-status/{id}', [CategoryController::class, 'Status'])->name('category.status');
    Route::delete('category-delete/{id}', [CategoryController::class, 'destroy'])->name('delete.category');
    Route::post('categories/change-status', [CategoryController::class, 'changeStatusAjax'])->name('category.changeStatusAjax');

    // Site Settings
    Route::get('sitesetting-list', [SiteSettingController::class, 'checktoredirect'])->name('sitesetting');
    Route::get('sitesetting-add', [SiteSettingController::class, 'Add'])->name('sitesetting.add');
    Route::post('sitesetting-store', [SiteSettingController::class, 'Store'])->name('sitesetting.store');
    Route::get('sitesetting-edit/{id}', [SiteSettingController::class, 'Edit'])->name('edit.sitesetting');
    Route::put('sitesetting-update/{id}', [SiteSettingController::class, 'Update'])->name('update.sitesetting');

    // Tags
    Route::get('tag-list', [TagController::class, 'List'])->name('tag.list');
    Route::get('tag-add', [TagController::class, 'Add'])->name('tag.add');
    Route::post('tag-store', [TagController::class, 'Store'])->name('tag.store');
    Route::get('tag-edit/{id}', [TagController::class, 'Edit'])->name('edit.tag');
    Route::put('tag-update/{id}', [TagController::class, 'Update'])->name('update.tag');
    Route::get('tag-status/{id}', [TagController::class, 'Status'])->name('tag.status');
    Route::post('tags/change-status', [TagController::class, 'changeStatusAjax'])->name('tag.changeStatusAjax');
    Route::delete('tags/{id}', [TagController::class, 'destroy'])->name('tag.delete');
});


/*
|--------------------------------------------------------------------------
| Admin + Author (Post Section)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'role:admin,author'])->prefix('admin')->group(function () {

    Route::get('/dashboard', [BackendController::class, 'Dashboard'])->name('dashboard');

    Route::get('posts', [BlogPostController::class, 'List'])->name('post.list');
    Route::get('post-add', [BlogPostController::class, 'Add'])->name('post.add');
    Route::post('post-store', [BlogPostController::class, 'Store'])->name('post.store');
    Route::get('post-edit/{id}', [BlogPostController::class, 'Edit'])->name('edit.post');
    Route::put('post-update/{id}', [BlogPostController::class, 'Update'])->name('update.post');

    Route::get('tra-post-list', [BlogPostController::class, 'Trashed'])->name('tra.post.list');
    Route::get('post-view/{id}', [BlogPostController::class, 'View'])->name('view.post');
    Route::get('post-status/{id}', [BlogPostController::class, 'Status'])->name('post.status');
    Route::get('post-delete/{id}', [BlogPostController::class, 'destroy'])->name('delete.post');
    Route::get('restore-post/{id}', [BlogPostController::class, 'Restore'])->name('restore.post');
    Route::get('per-post-delete/{id}', [BlogPostController::class, 'premanentdestroy'])->name('per.delete.post');
    Route::post('posts/change-status', [BlogPostController::class, 'changeStatusAjax'])->name('post.changeStatusAjax');

    Route::post('/tags', [TagController::class, 'tagstore'])->name('tags.store');
});


/*
|--------------------------------------------------------------------------
| Pages Resource (Admin)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::resource('pages', PageController::class);
});


/*
|--------------------------------------------------------------------------
| CMS Routes (Must be at bottom)
|--------------------------------------------------------------------------
*/

Route::get('/{slugurl}', [HomeController::class, 'Details'])->name('post.view');
Route::get('other/{slug}', [PageController::class, 'show'])->name('page.show');
