import {notFound, redirect} from "next/navigation";
import {auth} from "@/auth";
import {prisma} from "@/lib/db/prisma";
import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card";
import {Input} from "@/components/ui/input";
import {Label} from "@/components/ui/label";
import {Button} from "@/components/ui/button";
import {Settings} from "lucide-react";
import {updateBookingSettings} from "@/features/settings/server/actions";

type BookingSettingsPageProps = {
  params: Promise<{tenantSlug: string; locale: string}>;
};

export default async function BookingSettingsPage({params}: BookingSettingsPageProps) {
  const {tenantSlug, locale} = await params;
  const session = await auth();
  if (!session?.user?.id) redirect(`/${locale}/auth/sign-in`);

  const tenant = await prisma.tenant.findUnique({
    where: {slug: tenantSlug},
    include: {settings: true}
  });
  if (!tenant) notFound();

  const s = tenant.settings;

  async function handleSubmit(formData: FormData) {
    "use server";
    formData.set("tenantSlug", tenantSlug);
    await updateBookingSettings(formData);
  }

  return (
    <div className="max-w-2xl space-y-6">
      <div>
        <h1 className="text-2xl font-bold text-slate-900">Booking Settings</h1>
        <p className="mt-1 text-slate-500">Configure booking behavior for your company</p>
      </div>

      <form action={handleSubmit}>
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <Settings className="h-5 w-5 text-blue-600" />
              Booking Configuration
            </CardTitle>
          </CardHeader>
          <CardContent className="space-y-5">
            <div className="grid gap-4 sm:grid-cols-2">
              <div>
                <Label htmlFor="minimumRentalHours">Minimum Rental Hours</Label>
                <Input id="minimumRentalHours" name="minimumRentalHours" type="number"
                  defaultValue={s?.minimumRentalHours ?? 24} className="mt-1" />
              </div>
              <div>
                <Label htmlFor="maximumRentalDays">Maximum Rental Days</Label>
                <Input id="maximumRentalDays" name="maximumRentalDays" type="number"
                  defaultValue={s?.maximumRentalDays ?? 30} className="mt-1" />
              </div>
              <div>
                <Label htmlFor="defaultDepositAmount">Default Deposit Amount ($)</Label>
                <Input id="defaultDepositAmount" name="defaultDepositAmount" type="number" step="0.01"
                  defaultValue={s ? Number(s.defaultDepositAmount) : 0} className="mt-1" />
              </div>
              <div>
                <Label htmlFor="cancellationWindowHours">Cancellation Window (hours)</Label>
                <Input id="cancellationWindowHours" name="cancellationWindowHours" type="number"
                  defaultValue={s?.cancellationWindowHours ?? 24} className="mt-1" />
              </div>
              <div>
                <Label htmlFor="lateReturnFeePerHour">Late Return Fee / Hour ($)</Label>
                <Input id="lateReturnFeePerHour" name="lateReturnFeePerHour" type="number" step="0.01"
                  defaultValue={s ? Number(s.lateReturnFeePerHour) : 0} className="mt-1" />
              </div>
            </div>
            <div className="space-y-3">
              <div className="flex items-center gap-3 rounded-xl border border-slate-200 p-3">
                <input
                  id="bookingRequiresApproval"
                  name="bookingRequiresApproval"
                  type="checkbox"
                  defaultChecked={s?.bookingRequiresApproval ?? true}
                  className="h-4 w-4 rounded text-blue-600"
                />
                <div>
                  <Label htmlFor="bookingRequiresApproval" className="cursor-pointer font-medium">Require Approval</Label>
                  <p className="text-xs text-slate-500">Bookings must be manually approved before confirmation</p>
                </div>
              </div>
              <div className="flex items-center gap-3 rounded-xl border border-slate-200 p-3">
                <input
                  id="allowInstantBooking"
                  name="allowInstantBooking"
                  type="checkbox"
                  defaultChecked={s?.allowInstantBooking ?? false}
                  className="h-4 w-4 rounded text-blue-600"
                />
                <div>
                  <Label htmlFor="allowInstantBooking" className="cursor-pointer font-medium">Allow Instant Booking</Label>
                  <p className="text-xs text-slate-500">Customers can book instantly without approval</p>
                </div>
              </div>
            </div>
            <Button type="submit">Save Settings</Button>
          </CardContent>
        </Card>
      </form>
    </div>
  );
}
